Added a few Sphinx directives to the form API and template API docs.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11984 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Gary Wilson Jr 2009-12-25 20:51:30 +00:00
parent ac6273d675
commit b4f5e80cd1
2 changed files with 73 additions and 58 deletions

View File

@ -4,6 +4,8 @@
The Forms API The Forms API
============= =============
.. module:: django.forms.forms
.. currentmodule:: django.forms .. currentmodule:: django.forms
.. admonition:: About this document .. admonition:: About this document
@ -25,6 +27,8 @@ A :class:`Form` instance is either **bound** to a set of data, or **unbound**.
* If it's **unbound**, it cannot do validation (because there's no data to * If it's **unbound**, it cannot do validation (because there's no data to
validate!), but it can still render the blank form as HTML. validate!), but it can still render the blank form as HTML.
.. class:: Form
To create an unbound :class:`Form` instance, simply instantiate the class:: To create an unbound :class:`Form` instance, simply instantiate the class::
>>> f = ContactForm() >>> f = ContactForm()
@ -134,24 +138,25 @@ Dynamic initial values
.. attribute:: Form.initial .. attribute:: Form.initial
Use ``initial`` to declare the initial value of form fields at runtime. For Use :attr:`~Form.initial` to declare the initial value of form fields at
example, you might want to fill in a ``username`` field with the username of the runtime. For example, you might want to fill in a ``username`` field with the
current session. username of the current session.
To accomplish this, use the ``initial`` argument to a ``Form``. This argument, To accomplish this, use the :attr:`~Form.initial` argument to a :class:`Form`.
if given, should be a dictionary mapping field names to initial values. Only This argument, if given, should be a dictionary mapping field names to initial
include the fields for which you're specifying an initial value; it's not values. Only include the fields for which you're specifying an initial value;
necessary to include every field in your form. For example:: it's not necessary to include every field in your form. For example::
>>> f = ContactForm(initial={'subject': 'Hi there!'}) >>> f = ContactForm(initial={'subject': 'Hi there!'})
These values are only displayed for unbound forms, and they're not used as These values are only displayed for unbound forms, and they're not used as
fallback values if a particular value isn't provided. fallback values if a particular value isn't provided.
Note that if a ``Field`` defines ``initial`` *and* you include ``initial`` when Note that if a :class:`~django.forms.fields.Field` defines
instantiating the ``Form``, then the latter ``initial`` will have precedence. In :attr:`~Form.initial` *and* you include ``initial`` when instantiating the
this example, ``initial`` is provided both at the field level and at the form ``Form``, then the latter ``initial`` will have precedence. In this example,
instance level, and the latter gets precedence:: ``initial`` is provided both at the field level and at the form instance level,
and the latter gets precedence::
>>> class CommentForm(forms.Form): >>> class CommentForm(forms.Form):
... name = forms.CharField(initial='class') ... name = forms.CharField(initial='class')
@ -166,20 +171,21 @@ instance level, and the latter gets precedence::
Accessing "clean" data Accessing "clean" data
---------------------- ----------------------
Each ``Field`` in a ``Form`` class is responsible not only for validating data, .. attribute:: Form.cleaned_data
but also for "cleaning" it -- normalizing it to a consistent format. This is a
nice feature, because it allows data for a particular field to be input in Each field in a :class:`Form` class is responsible not only for validating
data, but also for "cleaning" it -- normalizing it to a consistent format. This
is a nice feature, because it allows data for a particular field to be input in
a variety of ways, always resulting in consistent output. a variety of ways, always resulting in consistent output.
For example, ``DateField`` normalizes input into a Python ``datetime.date`` For example, :class:`~django.forms.DateField` normalizes input into a
object. Regardless of whether you pass it a string in the format Python ``datetime.date`` object. Regardless of whether you pass it a string in
``'1994-07-15'``, a ``datetime.date`` object or a number of other formats, the format ``'1994-07-15'``, a ``datetime.date`` object, or a number of other
``DateField`` will always normalize it to a ``datetime.date`` object as long as formats, ``DateField`` will always normalize it to a ``datetime.date`` object
it's valid. as long as it's valid.
Once you've created a ``Form`` instance with a set of data and validated it, Once you've created a :class:`~Form` instance with a set of data and validated
you can access the clean data via the ``cleaned_data`` attribute of the ``Form`` it, you can access the clean data via its ``cleaned_data`` attribute::
object::
>>> data = {'subject': 'hello', >>> data = {'subject': 'hello',
... 'message': 'Hi there', ... 'message': 'Hi there',
@ -322,49 +328,56 @@ a form object, and each rendering method returns a Unicode object.
``as_p()`` ``as_p()``
~~~~~~~~~~ ~~~~~~~~~~
``Form.as_p()`` renders the form as a series of ``<p>`` tags, with each ``<p>`` .. method:: Form.as_p
containing one field::
>>> f = ContactForm() ``as_p()`` renders the form as a series of ``<p>`` tags, with each ``<p>``
>>> f.as_p() containing one field::
u'<p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p>\n<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>\n<p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>\n<p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>'
>>> print f.as_p() >>> f = ContactForm()
<p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p> >>> f.as_p()
<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p> u'<p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p>\n<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>\n<p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>\n<p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>'
<p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p> >>> print f.as_p()
<p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p> <p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p>
<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>
<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>
``as_ul()`` ``as_ul()``
~~~~~~~~~~~ ~~~~~~~~~~~
``Form.as_ul()`` renders the form as a series of ``<li>`` tags, with each .. method:: Form.as_ul
``<li>`` containing one field. It does *not* include the ``<ul>`` or ``</ul>``,
so that you can specify any HTML attributes on the ``<ul>`` for flexibility::
>>> f = ContactForm() ``as_ul()`` renders the form as a series of ``<li>`` tags, with each
>>> f.as_ul() ``<li>`` containing one field. It does *not* include the ``<ul>`` or
u'<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>\n<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>\n<li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>\n<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>' ``</ul>``, so that you can specify any HTML attributes on the ``<ul>`` for
>>> print f.as_ul() flexibility::
<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>
<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li> >>> f = ContactForm()
<li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li> >>> f.as_ul()
<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li> u'<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>\n<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>\n<li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>\n<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>'
>>> print f.as_ul()
<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>
<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>
<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>
``as_table()`` ``as_table()``
~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
Finally, ``Form.as_table()`` outputs the form as an HTML ``<table>``. This is .. method:: Form.as_table
exactly the same as ``print``. In fact, when you ``print`` a form object, it
calls its ``as_table()`` method behind the scenes::
>>> f = ContactForm() Finally, ``as_table()`` outputs the form as an HTML ``<table>``. This is
>>> f.as_table() exactly the same as ``print``. In fact, when you ``print`` a form object,
u'<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>\n<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>\n<tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>\n<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>' it calls its ``as_table()`` method behind the scenes::
>>> print f.as_table()
<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr> >>> f = ContactForm()
<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr> >>> f.as_table()
<tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> u'<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>\n<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>\n<tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>\n<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></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> >>> print f.as_table()
<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>
<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>
<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>
Styling required or erroneous form rows Styling required or erroneous form rows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -383,9 +396,9 @@ attributes::
class ContactForm(Form): class ContactForm(Form):
error_css_class = 'error' error_css_class = 'error'
required_css_class = 'required' required_css_class = 'required'
# ... and the rest of your fields here # ... and the rest of your fields here
Once you've done that, rows will be given ``"error"`` and/or ``"required"`` Once you've done that, rows will be given ``"error"`` and/or ``"required"``
classes, as needed. The HTML will look something like:: classes, as needed. The HTML will look something like::

View File

@ -502,12 +502,14 @@ The Python API
Django has two ways to load templates from files: Django has two ways to load templates from files:
``django.template.loader.get_template(template_name)`` .. function:: django.template.loader.get_template(template_name)
``get_template`` returns the compiled template (a ``Template`` object) for ``get_template`` returns the compiled template (a ``Template`` object) for
the template with the given name. If the template doesn't exist, it raises the template with the given name. If the template doesn't exist, it raises
``django.template.TemplateDoesNotExist``. ``django.template.TemplateDoesNotExist``.
``django.template.loader.select_template(template_name_list)`` .. function:: django.template.loader.select_template(template_name_list)
``select_template`` is just like ``get_template``, except it takes a list ``select_template`` is just like ``get_template``, except it takes a list
of template names. Of the list, it returns the first template that exists. of template names. Of the list, it returns the first template that exists.