【问题标题】:Parameters in Django Page RedirectionDjango 页面重定向中的参数
【发布时间】:2018-11-19 05:50:57
【问题描述】:

我在视图中有以下代码:

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 的“无聊”、“困惑”、“参与”的数量。请告诉我代码中的问题。我是否正确地将 url id 传递给 detail_affective?我的过滤方式正确吗?

【问题讨论】:

    标签: python django python-3.x redirect


    【解决方案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()
    

    您还需要在 urlpattern 中添加 id 作为 url agument 参数:

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

    【讨论】:

    • 它不工作。我认为有一些问题是将 url id 从提交传递到详细信息。如果我从参数中删除 id,它会起作用。
    • @PrabhjeetSingh 你能显示你的网址吗?您需要添加 id 作为 url 参数。
    • urlpatterns = [ path('create_url/',views.create_url,name='create_url'), path('submit_affective/',views.submit_affective,name='submit_affective'), path(' submit_cognitive/',views.submit_cognitive,name='submit_cognitive'), path('thanks/',views.thanks,name='thanks'), path('detail_affective/',views.detail_affective,name='detail_affective') , path('detail_cognitive/',views.detail_cognitive,name='detail_cognitive'), path('',views.index,name='index'), ]
    • @PrabhjeetSingh 检查更新的答案。您需要将id 添加到urlpattern。
    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 2010-10-06
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2015-04-12
    相关资源
    最近更新 更多