python - 如何将 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('/')
解决方案
文档中有一些示例:
通过传递视图的名称和可选的一些位置或关键字参数;URL 将使用 reverse() 方法进行反向解析:
def my_view(request):
...
return redirect('some-view-name', foo='bar')
https://docs.djangoproject.com/en/3.0/topics/http/shortcuts/#examples
推荐阅读
- javascript - 使用 chromecast 接收器应用程序的自定义应用程序框架进行更多自定义?
- c# - 如何通过搜索 ID 获取(DevExpress WPF)SpreadsheetControl 单元格值并将特定单元格值显示到文本框?
- php - 使用 Content-Type = application/x-www-form-urlencoded 发送 POST 请求时,POST 值为空
- mxgraph - mxgraph 库中是否已经存在用于为图像轮廓着色的函数?
- javascript - 散点图:以交互方式更改圆半径
- cobol - 将 DISPLAY 结果存储在 PIC X(n) 中
- javascript - 如何动态地将高度设置为模态?
- python - 为什么在finder中打不开桌面?
- javascript - Firebase 函数发送后无法设置标头
- c - 修改文件 udp-sink.c cooja 以拒绝除一个以外的所有 mots 的 msg