Last active
August 29, 2015 13:56
-
-
Save hguochen/8955240 to your computer and use it in GitHub Desktop.
Django form logic
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
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, | |
}) | |
def contact(request): | |
# if it's POST request it'll have data else it'll be unbound | |
form = ContactForm(request.POST or None) | |
if request.method == 'POST' and form.is_valid(): | |
# Process the data in form.cleaned_data | |
# ... | |
return HttpResponseRedirect('/thanks/') # Redirect after POST | |
return render_to_response('contact.html', { 'form': form, }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment