简体   繁体   English

Django页面重定向中的参数

[英]Parameters in Django Page Redirection

I have the following code in views: 我在视图中有以下代码:

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)

I want to show the number of "boring","confusing","engaging" in detail_affective only for that URL for which choice has been submitted in submit_affective . 我只想为在detail_affective中已提交选择的URL在detail_affective显示“无聊”,“混乱”,“参与”的submit_affective Please tell me the problem in my code. 请在代码中告诉我问题。 Am I passing the url id to detail_affective correctly? 我是否将网址ID正确传递给detail_affective? And is my way of filtering correct? 我的过滤方式正确吗?

You cannot use url.id expression as filter argument, just use url_id instead: 您不能使用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()

Also you need to add id as url agrument argument to the urlpattern: 另外,您需要将id作为url agrument参数添加到urlpattern:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM