From d94ed44c5df7313bce825d17a8f3e7dff9451365 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 29 Nov 2007 17:30:01 +0000 Subject: [PATCH] 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 --- AUTHORS | 1 + docs/newforms.txt | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/AUTHORS b/AUTHORS index 1625d820d4..f393dc7ab0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -188,6 +188,7 @@ answer newbie questions, and generally made Django that much better: krzysiek.pawlik@silvermedia.pl Joseph Kocherhans konrad@gwu.edu + knox kurtiss@meetro.com lakin.wecker@gmail.com Nick Lane diff --git a/docs/newforms.txt b/docs/newforms.txt index 5feb2e6a02..810982d788 100644 --- a/docs/newforms.txt +++ b/docs/newforms.txt @@ -753,6 +753,30 @@ For example:: +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:: + +
+
+ {% for field in form %} +
{{ field.label_tag }}{{ field.label }}{% if field.field.required %}*{% endif %}
+
{{ field }}
+ {% if field.help_text %}
{{ field.help_text }}
{% endif %} + {% if field.errors %}
{{ field.errors }}
{% endif %} + {% endfor %} +
+ +
+ +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 --------------------------------