开发者

Redirect to views according to form selection

User registration in my application is performed in steps. After submitting the form, some validation is performed and then register_prompt view is called. It renders a form, with two options - 'ok' and 'cancel'. Clicking ok will run registration, and clicking cancel should redirect to main page. Problem is that no matter which of the two I choose, I'm redirected to .../user/registration/function_1_or_2_name with a blank page (although I have specified url in HttpResponseRedirect ). How can I make it work ?

def register_prompt(request):
    context = RequestContext(request)
    return render_to_response('user/data_operations/alert.html', context_instance=context)

Form开发者_C百科 loaded on alert.html :

<form action="" method="post">
    <input type="submit" class="submit" name="submit" onClick="this.form.action='{% url register_new %}'" value="Ok" />
    <input type="submit" class="submit" name="submit" onClick="this.form.action='{% url redirect_home %}'" value="Cancel" />
</form>

Redirect views (maybe there is a better way to do that ?):

def redirect_home(request):
    return HttpResponseRedirect('/')

def register_new(request):
    (... registration magic here ...)
    return HttpResponseRedirect('/user/registration/complete/')

Finally url conf :

   url(r'^register_new/$', register_new, name="register_new"),
   url(r'^redirect_home/$', redirect_home, name="redirect_home"),
   url(r'^register_prompt/$', register_prompt, name="register_prompt"),

At first I was trying to add some abstract values to form's buttons (like 'action=ok'), and then catch them in register_prompt but it was a total disaster.


I'm not sure if that onClick="" hack is really valid (I'll let someone else weigh in on that), but did you try just using normal links, to see if it's that or something else?

<a href="{% url register_new %}">OK</a>
<a href="{% url register_home %}">Cancel</a>

(Or check out <button> if you really want it to look like buttons. Or just use two <form>s, each with a separate action="" attributes.)

Hum, other than that... You say ".../user/registration/function_1_or_2_name" -- what exactly does the {% url .. %}s give you? Does it not add the final / (which you require in your patterns)?

At first I was trying to add some abstract values to form's buttons (like 'action=ok'), and then catch them in register_prompt but it was a total disaster.

But you seem to want to manipulate the action attribute, so you can't catch anything in register_prompt()?

All in all I'd suggest you either use normal links, or actually handle the POST in the view that presented it:

def register_prompt(req):
    if 'ok' in req.POST:
        return register_new(req)
    if 'cancel' in req.POST:
        return register_cancel(req)
    return render_to_response('user/data_operations/alert.html',
                              context_instance=RequestContext(req))

Maybe something like that? Renaming your buttons to ok and cancel, and having the form post back to itself...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜