| | 755 | |
|---|
| | 756 | Highlighting required fields in templates |
|---|
| | 757 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 758 | |
|---|
| | 759 | You may wish to show a visitor which fields are required. Here is the above |
|---|
| | 760 | example modified to insert an asterix after the label of each required field:: |
|---|
| | 761 | |
|---|
| | 762 | <form method="post" action=""> |
|---|
| | 763 | <dl> |
|---|
| | 764 | {% for field in form %} |
|---|
| | 765 | <dt>{{ field.label_tag }}{{ field.label }}{% if field.field.required %}*{% endif %}</dt> |
|---|
| | 766 | <dd>{{ field }}</dd> |
|---|
| | 767 | {% if field.help_text %}<dd>{{ field.help_text }}</dd>{% endif %} |
|---|
| | 768 | {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %} |
|---|
| | 769 | {% endfor %} |
|---|
| | 770 | </dl> |
|---|
| | 771 | <input type="submit" /> |
|---|
| | 772 | </form> |
|---|
| | 773 | |
|---|
| | 774 | The ``{% if field.field.required %}*{% endif %}`` fragment is the relevant |
|---|
| | 775 | addition here. It adds the asterix only if the field is required. Note that we |
|---|
| | 776 | check ``field.field.required`` and not ``field.required``. In the template, |
|---|
| | 777 | ``field`` is a ``newforms.forms.BoundField`` instance, which holds the actual |
|---|
| | 778 | ``Field`` instance in its ``field`` attribute. |
|---|