我尝试使用重定向函数传递变量,但它没有返回任何值。
def one:
# define location variable here
return redirect(getting_started_info, location=location)
def getting_started_info(request, location=''):
location = location
...
为什么重定向中的变量没有传递?
请记住,
redirect()
并不是直接调用您的视图,它实际上是向客户端浏览器发送302 Found
状态(如果您使用301 Moved Permanently
,则为permanent=True
)以及重定向到不同URL的指令。在本例中,该 URL 解析为 getting_started_info
视图。
我相信要使其工作,必须存在一个映射到
getting_started_view
并使用其 location
位置参数的 urlconf。这很可能通过命名团体发生。
来自 django 1.8 文档 entry on
redirect()
:
论点可能是:
- 模型:将调用模型的 get_absolute_url() 函数。
- 视图名称,可能带有参数: urlresolvers.reverse 将用于反向解析名称。
- 绝对或相对 URL,将按原样用于重定向位置。
return HttpResponseRedirect(reverse('getting_started_info', kwargs={'location': location}))
您还可以使用会话或 cookie 传递某些变量的值。