首页 > 解决方案 > 如何将 my_dict 传递给重定向,例如 redirect('home',my_dict)

问题描述

我想将此错误传递到模板中,但它不起作用,

def addcomment(request):
name = request.POST['name']
comment = request.POST['comment']
if name=="" and comment=="":
    my_dict = {'error':'blank fields not allowed'}

    return redirect('home',my_dict)

Comment(name = request.POST['name'],comment = request.POST['comment']).save()
return HttpResponseRedirect('/')

标签: pythonhtmldjangowebbackend

解决方案


文档中有一些示例:

通过传递视图的名称和可选的一些位置或关键字参数;URL 将使用 reverse() 方法进行反向解析:

def my_view(request):
   ...
   return redirect('some-view-name', foo='bar')

https://docs.djangoproject.com/en/3.0/topics/http/shortcuts/#examples


推荐阅读