在 Django 中传递重定向变量
我试图使用重定向函数传递一个变量,但它没有返回任何值。
def one:
# define location variable here
return redirect(getting_started_info, location=location)
def getting_started_info(request, location=''):
location = location
...
为什么重定向中的变量没有传递?
I'm trying to pass a variable using the redirect function, but it is returning none.
def one:
# define location variable here
return redirect(getting_started_info, location=location)
def getting_started_info(request, location=''):
location = location
...
Why is the variable in the redirect not passing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请记住,
redirect()
并不是直接调用您的视图,它实际上是向客户端浏览器发送302 Found
状态(301 Moved Permanently 如果您使用
permanent=True
) 以及重定向到不同 URL 的指令。在本例中,该 URL 解析为getting_started_info
视图。我相信要实现此功能,必须存在一个映射到 getting_started_view 并使用其 location 参数的 urlconf。这很可能通过命名组发生。
来自 django 1.8 文档 条目
redirect( )
:Remember that
redirect()
isn't doing a direct call to your view, it's actually sending the client's browser a302 Found
status (301 Moved Permanently
if you usepermanent=True
) with an instruction to redirect to a different URL. In this case, that URL is one that resolves to thegetting_started_info
view.I believe that for this to work, there must exist a urlconf which maps to
getting_started_view
and which uses itslocation
positional argument. This will most likely occur through named groups.From the django 1.8 docs entry on
redirect()
:您还可以使用会话或 cookie 传递某些变量的值。
you may also pass a value of some variable using sessions or cookie.