Tell if a Django Field is required from template

DjangoDjango Templates

Django Problem Overview


I'm rendering a form. I would like to put a mark next to all the fields that must be filled in. Simple stuff usually... but I've no idea how to access that information!

{% if field.required %}REQUIRED!!{% endif %}

doesn't bring any love...

Django Solutions


Solution 1 - Django

{% if field.field.required %}

From this snippet

Solution 2 - Django

I usually write out the html for each of my form elements, so I have a little more control over each one. Then you can add an * for required or REQUIRED!!.

<p><label>Title: *<br />
{% if form.title.errors %}<ul class="errorlist">{{ form.title.errors }}</ul>{% endif %}
{{form.title}}
</label></p>    

<p><label>Category:<br />
{% if form.category.errors %}<ul class="errorlist">{{ form.category.errors }}</ul>{% endif %}
{{form.category}}
</label></p>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOliView Question on Stackoverflow
Solution 1 - DjangoNed BatchelderView Answer on Stackoverflow
Solution 2 - DjangoJoeView Answer on Stackoverflow