Created
January 30, 2013 17:38
-
-
Save Wilfred/4675002 to your computer and use it in GitHub Desktop.
A tidy way of validating forms in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import redirect, render_to_response | |
from django.template import RequestContext | |
from .forms import UserProfileForm | |
def my_view(request): | |
if request.method == "POST": | |
form = UserProfileForm(request.POST) | |
if form.is_valid(): | |
form.save() | |
redirect(reverse("relevant_success_url")) | |
else: | |
form = UserProfileForm() | |
template_vars = { | |
'foo': "bar", | |
'form' : form | |
} | |
return render_to_response("my_template.html", template_vars, | |
context_instance=RequestContext(request)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment