Django页面重定向中的参数

2024-09-28 16:58:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在视图中有以下代码:

def submit_affective(request):
if request.method == 'POST':
    choice=request.POST['aff_choices']
    urlid=request.POST['url_list']
    url = Url.objects.get(id=urlid)
    aff_result=Affective.objects.create(
        affective=choice, url=url
        )
    return redirect('detail_affective',id=url.id)

def detail_affective(request,id):
boring_count=Affective.objects.filter(affective='Boring',url.id=id).count()
confusing_count=Affective.objects.filter(affective='Confusing',url.id=id).count()
engaging_count=Affective.objects.filter(affective='Engaging',url.id=id).count()
context = {
    'boring_count':boring_count,
    'confusing_count':confusing_count,
    'engaging_count':engaging_count,
}
return render(request,'feedback/detail_affective.html',context)

我只想显示detail_affective中的“无聊”、“困惑”、“参与”的数量,该URL已在submit_affective中提交选择。请告诉我代码中的问题。我是否正确地将url id传递给详细信息?我的过滤方法正确吗


Tags: 代码idurlobjectsrequestdefcountfilter
1条回答
网友
1楼 · 发布于 2024-09-28 16:58:25

不能使用url.id表达式作为筛选参数,只需使用url_id

boring_count=Affective.objects.filter(affective='Boring',url_id=id).count()
confusing_count=Affective.objects.filter(affective='Confusing',url_id=id).count()
engaging_count=Affective.objects.filter(affective='Engaging',url_id=id).count()

您还需要将id作为url参数添加到urlpattern中:

path('detail_affective/<int:id>',views.detail_affective,name='detail_affective')

相关问题 更多 >