返回无效表单时如何维护下一个参数?
(我不想因各种原因实现ajax)
以下是我的观点代码:
def registration(request):
if request.method == 'POST':
form = MyForm(request.POST)
redirect_to = request.REQUEST.get('next','')
if form.is_valid():
#Do stuff
return HttpResponseRedirect(redirect_to if redirect_to != ''
else reverse('named_url'))
#Next parameter lost here!!
return render(request, 'registration.html', {'form' : form})
else:
return render(request, 'registration.html', {'form' : MyForm()})
答案 0 :(得分:2)
您应该将其作为另一个值传递到模板上下文中,然后在模板中将其用作表单标记中的action
参数:
<form method="POST" action="?next={{ next }}">