【问题标题】:how to pass my_dict to redirect, like redirect('home',my_dict)如何将 my_dict 传递给重定向,例如 redirect('home',my_dict)
【发布时间】:2020-07-13 06:39:12
【问题描述】:

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

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('/')

【问题讨论】:

  • 重定向使浏览器向另一个视图发出另一个请求。这可以由不同的服务器处理。要跨多个请求传递消息,请使用 django messages framework
  • 是的,它的工作原理:),它有办法在添加到消息后删除消息吗?

标签: python html django web backend


【解决方案1】:

文档中有一些示例:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多