Is there any clever way to make django forms render field with asterisks after fields that are required? Or to provide some other clever for to mark required fields? I wouldn't like to have to do it again in a template if I already set a field as required in the form.
5 Answers
As of Django 1.2, if your form has an attribute named required_css_class
, it will be added to BoundField.css_classes
for required fields. You can then use CSS to style the required parts of the form as desired. A typical use case:
# views.py
class MyForm(django.forms.Form):
required_css_class = 'required'
…
…
/* CSS */
th.required { font-weight: bold; }
…
<!-- HTML -->
<tr>
<th class="{{form.name.css_classes}}">{{form.name.label_tag}}</th>
<td>{{form.name.errors}}{{form.name}}</td>
</tr>
If you use Form.as_table()
, Form.as_ul
, and Form.as_p
, they do this automatically, adding the class to <tr>
, <li>
, and <p>
, respectively.

- 17,438
- 13
- 70
- 88
-
10Note you can use css content directive to add an asterisk using same this method: `th.required:after { content: '*' }` – Gringo Suave Nov 11 '11 at 20:58
You also can use a field.field.required property:
{% for field in form %}
{{field.label}} {% if field.field.required %} * {% endif %}
{{field}}
{{field.errors}}
{% endfor %}

- 111
- 1
- 6
-
Perfect. This solution will be required in case you are looping over the form fields and rendering every field separately using `{% for field in form %}` etc., instead of rendering the whole form using `{{ form }}` – Nitin Nain Jan 08 '21 at 07:29
The best way for such purposes I have found is to render form's output via an html template. How to do this is described here. By luck, the example puts an asterisk after required fields just like you want.

- 9,536
- 7
- 60
- 68
-
None of my forms is rendered {{ form }}, I explicitly call fields, because I usually display only subset of form's fields. I need something lower, like an attribute of a field or a widget. – gruszczy Aug 10 '09 at 10:21
-
If you are talking about modelforms and using 'fields' property of the Meta class to determine a subset of fields to display, this will work. If you have non-model forms and have hidden fields, again it will work. I am in a haste, sorry if I am missing something? – shanyu Aug 10 '09 at 10:29
-
I have a single Form that is used in many templates and I usually call all the fields I need in the template. That's why I can't use templated rendering. But it seems it would be better, If I just created some subclasses with Meta and then use this templating. That's good idea, I have to think about it. – gruszczy Aug 10 '09 at 10:49
-
I'm looping through all fields of a form, so this did wonders for me :) – Eduard Luca May 21 '14 at 14:40
I use a template tag to render form fields with their labels and errors, plus an asterisk and a CSS class for required fields. There are various snippets available to do it on www.djangosnippets.org

- 588,541
- 66
- 880
- 895
-
So do I at the moment, I mean I use a filter to do this. Still, this isn't very dry. I'd prefer to put this setting into the form (some 'mark_required=True' in form). – gruszczy Aug 10 '09 at 12:57
Personny I tried something like this, i.e. overriding the default template tag (this adds a red asterisk in all the admin interface for required not-readonly fields. Then i also defined a 2nd tag for my views that mix inline and block labels. See the code below (copy/paste of Django source code with some modifications):
In settings.py :
from django.forms.util import flatatt
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
import django.forms.forms as django_forms
def custom_label_tag(self, contents=None, attrs=None):
"""
Wraps the given contents in a <label>, if the field has an ID attribute.
Does not HTML-escape the contents. If contents aren't given, uses the
field's HTML-escaped label.
If attrs are given, they're used as HTML attributes on the <label> tag.
"""
contents = contents or conditional_escape(self.label)
widget = self.field.widget
id_ = widget.attrs.get('id') or self.auto_id
if id_:
attrs = attrs and flatatt(attrs) or ''
if self.field.required:
label = unicode(contents)
label_suffix = ""
if label[-1] == ":":
label = label[:-1]
label_suffix += ":"
contents = u'<label for="%s"%s>%s<span style="color:red;">*</span>%s</label>' % (widget.id_for_label(id_), attrs, label, label_suffix)
else:
contents = u'<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, unicode(contents))
return mark_safe(contents)
def custom_inline_label_tag(self, contents=None, attrs=None):
if attrs:
if "class" in attrs.keys():
attrs["class"] += " inline"
else:
attrs["class"] = "inline"
else:
attrs = {"class": "inline"}
return self.label_tag(contents, attrs)
django_forms.BoundField.label_tag = custom_label_tag # override django method
django_forms.BoundField.inline_label_tag = custom_inline_label_tag
In my templates
<p>{{ form.fieldname.errors}}{{ form.fieldname.inline_label_tag }}{{form.fieldname}}</p>
OR
<p>{{ form.fieldname.errors}}{{ form.fieldname.label_tag }}{{form.fieldname}}</p>

- 2,402
- 17
- 16