Turbogears 2 - validating forms to the same url
I have a controller for a contact page similar to the following:
@expose('project.templates.contacts')
def contact(self, **kw):
return dict( form=contact_form )
Upon submission the form is validated using the following controller method:
@validate(form=contact_form, error_handler=contact)
@expose()
def processContact(self, **kw):
# Do some processing on the contact form
redirect('contact')
The is the setup advocated by many online tutorials (such as http://turbogears.org/2.0/docs/main/FormBasics.html).
My issue is the URL that is exposed when the contact form is submitted with bad data and @validate calls the error_handler method.
I.e.
- "http://domain/contact" - the user goes to the contact page and fills in the form and click submit
- "http://domain/contact" - if there are no errors in the form, the user is successfully redirected to the contact page.
- "http://domain/processContact" - if there are errors in the form, the contact function is called but there is no redirection from the exposed processContact 'page' so the url remains the same.
I'm looking for a way (t开发者_StackOverflow社区he correct way?) to prevent the user from having to see "http://domain/processContact". Ideally the user should only ever see "http://domain/contact".
[this is not an answer, but more of a comment, but for reputation issues, I'm not allowed to add comments] Your description of the problem seemed a bit vague to me. As far as I know, the user won't see "http://domain/processContact" as doesnt have any link in its expose. In other words, what I understand from your codes is that: 1- in case of error in the form, the user will be redirected to contact page where you access the entered inputs 2- in case of success, the user will be redirected to a new contact page
Id I'm mistaken, please clarify your problem a bit more.
Set as form action the contact page itself, add a @validate to it but don't provide the error_handler. If you omit it, instead of calling the error_handler the flow goes on and you just find your errors in tmpl_context.form_errors.
Then inside your controller you have to handle three cases:
- tg.request.method is GET -> Render the form
- tg.request.method is POST and tmpl_contet.form_errors is not empty -> Render the form
- tg.request.method is POST and no tmpl_contet.form_errors -> process submit + redirect
精彩评论