簡體   English   中英

Django頁面重定向中的參數

[英]Parameters in Django Page Redirection

我在視圖中有以下代碼:

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在detail_affective顯示“無聊”,“混亂”,“參與”的submit_affective 請在代碼中告訴我問題。 我是否將網址ID正確傳遞給detail_affective? 我的過濾方式正確嗎?

您不能使用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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM