diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-05-19 19:13:39 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-05-19 19:13:39 +0000 |
| commit | 0f424f50849930f92ab37638cfa1e34caac09877 (patch) | |
| tree | 44b029a3d88629e208f3187298ccf8348880abb9 | |
| parent | d8db013eea14e69222810deea9a7e290b99966b4 (diff) | |
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
| -rw-r--r-- | docs/newforms.txt | 109 |
1 files changed, 109 insertions, 0 deletions
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:: <p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p> <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p> +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 ``<ul>`` for flexibility:: <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li> <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li> +In a template, you can invoke this if the form has been handed into the +context. For example:: + + {{ f.as_ul }} + ``as_table()`` ~~~~~~~~~~~~~~ @@ -421,6 +457,18 @@ calls its ``as_table()`` method behind the scenes:: <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr> +In a template, you can invoke this if the form has been handed into the +context. For example:: + + {{ f.as_table }} + +which is the same as + +:: + + {{ f }} + + Configuring HTML ``<label>`` tags ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -602,6 +650,67 @@ when printed:: >>> str(f['subject'].errors) '' +In the templates +---------------- + +Using the above example, let's put this into a view and show how you can use +these parts from the template designer's point of view. Assuming you start +with a view like this:: + + def contact(request): + form = ContactForm() + if request.method == 'POST': + new_data = request.POST.copy() + form = ContactForm(new_data) + if form.is_valid(): + # do form processing here... + return render_to_response('contact.html', {'form': form}) + +...you can have a simple template that uses the shortcuts ``form.as_ul``, +``form.as_p``, or ``form.as_table`` (which is the default rendering method for +a form variable). An example ``contact.html`` template:: + + <form method="POST"> + {{ form }} + </form> + +Equivalently, you could write:: + + <form method="POST"> + {{ form.as_table }} + </form> + +If you wanted to work with the individual inputs of the form, you can either +call out the fields directly or iterate over them:: + + <form method="POST"> + <dl> + {% for field in form %} + <dt>{{ field.label }}</dt> + <dd>{{ field }}</dd> + <dd>{{ field.help_text }}</dd> + {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %} + {% endfor %} + </dl> + </form> + +Alternatively:: + + <form method="POST"> + <ul class="myformclass"> + <li>{{ form.sender.label }} {{ form.sender.label }}</li> + <li class="helptext" >{{ form.sender.help_text }}</li> + {% if form.sender.errors %}<ul class="errorlist">{{ form.sender.errors }}</dd>{% endif %} + + <li>{{ form.subject.label }} {{ form.subject.label }}</li> + <li class="helptext" >{{ form.subject.help_text }}</li> + {% if form.subject.errors %}<ul class="errorlist">{{ form.subject.errors }}</dd>{% endif %} + + ... + </ul> + </form> + + Subclassing forms ----------------- |
