我如何渲染模板,然后重定向到Django中的特定URL?

时间:2022-09-07 15:48:27

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.

我对django观点有疑问。我有一个URL /signup.html,它有一个视图并显示一个表单。这个表单的动作指向/ account / create所以当一切正常时我会重定向到恭喜页面,但是当表单提交它无效时,我需要用错误字典回到最后一个url但是当我做render_to_response时地址栏中的网址是帐户/创建的,应该是/signup.html。

Here is 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.

编辑:如果我发布相同的网址(signup.html),当我重新加载页面我有多个帖子提交,我想阻止这个。

2 个解决方案

#1


1  

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

为什么不发布到相同的URL(signup.html),然后只在成功时重定向?

#2


0  

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

我很困惑为什么你需要去/ account / create。你不能这样做:

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.

我不确定你是否真的需要去create_account(),但如果你不这样做,这应该适合你。

#1


1  

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

为什么不发布到相同的URL(signup.html),然后只在成功时重定向?

#2


0  

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

我很困惑为什么你需要去/ account / create。你不能这样做:

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.

我不确定你是否真的需要去create_account(),但如果你不这样做,这应该适合你。