Django无效表单,保持下一个参数

时间:2013-03-27 20:30:25

标签: django django-forms

返回无效表单时如何维护下一个参数?

(我不想因各种原因实现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()})

1 个答案:

答案 0 :(得分:2)

您应该将其作为另一个值传递到模板上下文中,然后在模板中将其用作表单标记中的action参数:

<form method="POST" action="?next={{ next }}">