Skip to content

Instantly share code, notes, and snippets.

@hguochen
Last active August 29, 2015 13:56
Show Gist options
  • Save hguochen/8955240 to your computer and use it in GitHub Desktop.
Save hguochen/8955240 to your computer and use it in GitHub Desktop.
Django form logic
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