开发者

How i can render a template and then redirect to particular URL in Django?

i've a problem with django views. I have a URL /signup.html that have a view and display a form. The action of this form points to /account/create so when everything it's ok i do a redirect to congrats page, but when the form submitted it's invalid, i need to back to the last url with a dictionary of errors but when i do render_to_response the url in the address bar it's account/create and should /signup.html.

Here is开发者_如何学Go the code:

def signup(request):
    return render_to_response('main/signup.html' , {} , context_instance=RequestContext(request))

def create_account(request):
    if request.method == 'POST':
        form = FastSignupForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return redirect('/account/congratulations' , {} , context_instance=RequestContext(request))
    else:
        form = FastSignupForm();

    return render_to_response('main/signup.html', {'form':form} , context_instance=RequestContext(request))

def congrats(request):
    return render_to_response('main/congrats.html', {}, context_instance=RequestContext(request))

What i'm doing wrong ?

EDIT: If i post over the same url (signup.html), when a i reload the page i've multiple post submits and i want to prevent this.


Why not post to the same URL (signup.html) then only redirect when successful?


I'm confused as to why you need to go to /account/create. Can't you just do this:

views.py

def signup(request):
    if request.method == 'POST':
        form = FastSignupForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return redirect('/account/congratulations' , {} , 
                         context_instance=RequestContext(request))
    else:
        form = FastSignupForm();

    return render_to_response('main/signup.html', {'form':form} , 
                 context_instance=RequestContext(request))

def congrats(request):
    return render_to_response('main/congrats.html', {}, 
                 context_instance=RequestContext(request))

main/signup.html

...
<form method="post" action=".">
    ...
</form>
...

I'm not sure if you really need to go to create_account() or not, but if you don't this should work for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜