Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » How to write good form validation in Django? [RSS Feed]

#1 Dec. 11, 2010 01:13:12

Ali G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to write good form validation in Django?


I've seen Django's samples and I can see they have decent error
handling. However I want to see if there is yet a better approach, a
general pattern to handle form validation errors in Django. This is
the sample I found here:

def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST
data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/thanks/') # Redirect after
POST
else:
form = ContactForm() # An unbound form

return render_to_response('contact.html', {
'form': form,
})
Now I was wondering:

How can the view in "/thanks/" be sure that the form was validated?
Are there any common ways to pass the successful validation of the
form to the next view? Or do I need to do something manually such as
setting a flag in request's session?

How can one write this code in a way that when form is NOT valid and
the page is shown with errors upon submission, if user refreshes the
browser it wouldn't ask the user if they want to POST data again?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 11, 2010 05:45:32

Christophe P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to write good form validation in Django?


On Dec 10, 2010, at 4:01 PM, Ali Ghaffari wrote:

> How can the view in "/thanks/" be sure that the form was validated?
> Are there any common ways to pass the successful validation of the
> form to the next view? Or do I need to do something manually such as
> setting a flag in request's session?

It depends on what your concern is. If the "/thanks/" view is going to do an
operation that it simply should not do unless the form was validated, then,
yes, you should make sure the state of the application (via the database,
session, or something else) is sufficient that the "/thanks/" form can confirm
that the operation was really done before doing its part.

If "/thanks/" doesn't do anything to the state of the application, then the
worst that could happen is that the user accesses it directly, rather than on a
redirect, and is told thanks when the application doesn't really mean it. This
may or may not be worth guarding against.

> How can one write this code in a way that when form is NOT valid and
> the page is shown with errors upon submission, if user refreshes the
> browser it wouldn't ask the user if they want to POST data again?

Well, a refresh in this case on the user's part *is* a request to repost the
form data again. What's the condition you are trying to avoid in this
sitaution?

--
-- Christophe Pettus
x...@thebuild.com

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » How to write good form validation in Django? [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support