Modified the example views in newforms.txt to include a HttpResponseRedirect (as per best practice), and remove a duplicated form instantiation.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5295 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2007-05-20 02:18:22 +00:00
parent 0f424f5084
commit d43522b077
1 changed files with 6 additions and 3 deletions

View File

@ -321,6 +321,7 @@ Putting this all together, here is a simple view method that uses our contact
form:: form::
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django import newforms as forms from django import newforms as forms
class ContactForm(forms.Form): class ContactForm(forms.Form):
@ -334,6 +335,7 @@ form::
f = ContactForm(request.POST) f = ContactForm(request.POST)
if f.is_valid: if f.is_valid:
# ... do something with f.cleaned_data # ... do something with f.cleaned_data
return HttpResponseRedirect('/url/on_success/')
else: else:
f = ContactForm() f = ContactForm()
return render_to_response('contact.html', {'form': f}) return render_to_response('contact.html', {'form': f})
@ -658,12 +660,13 @@ these parts from the template designer's point of view. Assuming you start
with a view like this:: with a view like this::
def contact(request): def contact(request):
form = ContactForm()
if request.method == 'POST': if request.method == 'POST':
new_data = request.POST.copy() form = ContactForm(request.POST)
form = ContactForm(new_data)
if form.is_valid(): if form.is_valid():
# do form processing here... # do form processing here...
return HttpResponseRedirect('/url/on_success/')
else:
form = ContactForm()
return render_to_response('contact.html', {'form': form}) return render_to_response('contact.html', {'form': form})
...you can have a simple template that uses the shortcuts ``form.as_ul``, ...you can have a simple template that uses the shortcuts ``form.as_ul``,