1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #5854 -- Added an example to the newforms documentation showing how to

highlight required fields (which should also provide enough clues for
accessing other attributes on newforms.Field subclasses. Thanks,
christobzr@gmail.com.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6740 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-11-29 17:30:01 +00:00
parent 8d29fabe31
commit d94ed44c5d
2 changed files with 25 additions and 0 deletions

View File

@ -188,6 +188,7 @@ answer newbie questions, and generally made Django that much better:
krzysiek.pawlik@silvermedia.pl
Joseph Kocherhans
konrad@gwu.edu
knox <christobzr@gmail.com>
kurtiss@meetro.com
lakin.wecker@gmail.com
Nick Lane <nick.lane.au@gmail.com>

View File

@ -753,6 +753,30 @@ For example::
</ul>
</form>
Highlighting required fields in templates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You may wish to show a visitor which fields are required. Here is the above
example modified to insert an asterix after the label of each required field::
<form method="post" action="">
<dl>
{% for field in form %}
<dt>{{ field.label_tag }}{{ field.label }}{% if field.field.required %}*{% endif %}</dt>
<dd>{{ field }}</dd>
{% if field.help_text %}<dd>{{ field.help_text }}</dd>{% endif %}
{% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %}
{% endfor %}
</dl>
<input type="submit" />
</form>
The ``{% if field.field.required %}*{% endif %}`` fragment is the relevant
addition here. It adds the asterix only if the field is required. Note that we
check ``field.field.required`` and not ``field.required``. In the template,
``field`` is a ``newforms.forms.BoundField`` instance, which holds the actual
``Field`` instance in its ``field`` attribute.
Binding uploaded files to a form
--------------------------------