django: Mark field as required on a form

0 Comments

A simple little feature which is lacking from the automatic form generation is marking something as required.

It's a bit of a shame that you have to override the whole form to get this working, but it gives more flexibility to how you want the required markings to be displayed.

01.{% for field in form %}
02.<div>
03.  <label for="{{ field.auto_id }}">
04.    {% if field.field.required %}<span class="required">{{ field.label }}</span>
05.    {% else %}{{ field.label }}{% endif %}
06.  </label>
07.  {{ field }}
08.  {{ field.errors }}
09.</div>
10.{% endfor %}

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog