Django页面重定向中的参数

时间:2018-06-09 09:52:15

标签: python django python-3.x redirect

我在视图中有以下代码:

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中仅显示已在submit_affective中提交选择的网址,显示“无聊”,“令人困惑”,“参与”的数量。请告诉我我的代码中的问题。我是否正确地将url id传递给detail_affective?我的过滤方式是否正确?

1 个答案:

答案 0 :(得分:1)

您不能将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 agrument参数添加到urlpattern:

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