From 0f424f50849930f92ab37638cfa1e34caac09877 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 19 May 2007 19:13:39 +0000 Subject: [PATCH] Fixed #4232 -- Added example usages to the newforms documentation. Thanks, Joe Heck. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5294 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/newforms.txt | 109 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/docs/newforms.txt b/docs/newforms.txt index ed43670960..4d2400afcc 100644 --- a/docs/newforms.txt +++ b/docs/newforms.txt @@ -313,6 +313,31 @@ record, here's what happens with unbound forms:: ... AttributeError: 'ContactForm' object has no attribute 'cleaned_data' + +Example View +~~~~~~~~~~~~ + +Putting this all together, here is a simple view method that uses our contact +form:: + + from django.shortcuts import render_to_response + from django import newforms as forms + + class ContactForm(forms.Form): + subject = forms.CharField(max_length=100) + message = forms.CharField() + sender = forms.EmailField() + cc_myself = forms.BooleanField() + + def contact(request): + if request.POST: + f = ContactForm(request.POST) + if f.is_valid: + # ... do something with f.cleaned_data + else: + f = ContactForm() + return render_to_response('contact.html', {'form': f}) + Outputting forms as HTML ------------------------ @@ -389,6 +414,12 @@ containing one field::

+In a template, you can invoke this if the form has been handed into the +context. For example:: + + {{ f.as_p }} + + ``as_ul()`` ~~~~~~~~~~~ @@ -405,6 +436,11 @@ so that you can specify any HTML attributes on the ``