Joh*_*ash 11 django django-templates django-models django-forms django-views
我在页面x和页面y上有一个按钮,重定向到页面z.在页面z,我有一个需要填写的表单.保存后,我想重定向到页面x或y(我最初使用的那个).
通常,您在视图中使用"重定向",并指定要重定向到的页面.但是在这样的情况下你会怎么做?
有任何想法吗?
谢谢!
Tim*_*ony 11
您可以使用GET参数来跟踪您到达页面z的页面.因此,当您正常到达页面z时,我们会记得我们来自哪个页面.当您在页面z上处理表单时,我们使用以前保存的信息进行重定向.所以:
第y页上的按钮/链接应包含一个参数,其值为当前URL:
<a href="/page_z/?from={{ request.path|urlencode }}" />go to form</a>
Run Code Online (Sandbox Code Playgroud)
然后在page_z的视图中,您可以将其传递到模板:
def page_z_view(self, request):
...
return render_to_response('mytemplate.html', { 'from' : request.GET.get('from', None) })
Run Code Online (Sandbox Code Playgroud)
并在您的表单模板中:
<form action="{% if from %}?next={{ from }}{% endif %}" />
...
Run Code Online (Sandbox Code Playgroud)
所以现在表单 - 提交时 - 将传递一个next参数,指示一旦表单成功提交后返回的位置.我们需要恢复视图才能执行此操作:
def page_z_view(self, request):
...
if request.method == 'POST':
# Do all the form stuff
next = request.GET.get('next', None)
if next:
return redirect(next)
return render_to_response('mytemplate.html', { 'from' : request.GET.get('from', None)}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9113 次 |
| 最近记录: |