【发布时间】:2014-06-09 06:10:48
【问题描述】:
在我的 djangoApp 中,views.py 我有:
status = True
context = {'status': status ,
"form":ProfileForm()
}
return render(request, "sample/Newsample.html", context)
如果我想使用带有重定向功能的上下文怎么办?例如:
status = True
context = {'status': status ,
"form":ProfileForm()
}
return redirect("sample/Newsample.html", context) - this does not work!!
如何在重定向功能中使用上下文?
【问题讨论】:
-
你到底想做什么?
-
我的模板中有一个名为 status 的变量,当 status var 为 true 时会显示内容。我想通过 redirect() 函数将此状态变量传递给 Newsample.html。
-
您将重定向与渲染混淆了。重定向将您发送到一个新的网址。 Django 将处理此 url,因为它处理任何其他 http 请求并在适当的视图中创建任何上下文。您可以将 url、url 名称或模型(只要它定义为 get_absoluteurl() )传递给重定向。文档:docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect 您通常不会在重定向视图中创建上下文对象。好吧,如果您的视图接受一个上下文变量(这将小于正统),您可以发送一个,但这有点必要
-
但如果您愿意,您也可以将它们与一些 get 变量一起发送