我有以下模板,其中添加了获取请求
function openProject(projectId) {
fetch(`/segmentation/test/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
mode: 'same-origin',
})
.then(response => {
if (response.ok) {
console.log("Всё супер");
} else {
console.error('Ошибка при открытии проекта');
}
})
.catch(error => {
console.error('Произошла ошибка:', error);
});
}
以及下一个视图 在其中我收集了与该项目相关的图像和标签的所有信息
def test(request):
projectId = request.GET.get('projectId')
if projectId:
project = Project.objects.get(pk=projectId)
_tags = project.tags.all()
_images = project.images.all()
print(_images)
polygons = []
colorPol = []
for i in range(len(_images)):
polygons.append([])
colorPol.append([])
for i in range(len(_images)):
for rect in Rect.objects.all().filter(inImage=i+1):
tempArr = []
colorPol[i].append(rect.tag.Name)
for p in rect.points.all():
tempArr.append([p.x, p.y])
polygons[i].append(tempArr)
else:
_tags = []
_images = []
polygons = []
colorPol = []
return render(request,
'segmentation/testDraw.html',
{'tags': _tags, 'images': _images, 'title': 'Test Draw', 'rectForImage': polygons,
'colorPolArr': colorPol})
我的网址:
app_name = 'segmentation'
urlpatterns = [
path('test/', test, name='testDraw'),
path('projects/', project_show, name='project_show'),
]
请帮我解决这个问题
我尝试更改发布请求,但据我了解这并不重要
要在处理 GET 请求后在 Django 视图中实现渲染,通常需要遵循以下步骤:
这是一个示例实现:
views.py:
from django.shortcuts import render
from .models import YourModel
def your_view(request):
# Handle GET request logic
if request.method == 'GET':
# Retrieve data from the database
data = YourModel.objects.all()
# Pass data to the template for rendering
return render(request, 'your_template.html', {'data': data})
your_template.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Template</title>
</head>
<body>
<h1>Your Data</h1>
<ul>
{% for item in data %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</body>
</html>
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('your-url/', views.your_view, name='your_view'),
]