Day 32 of 100 Days of Django: Styling Errors in Form Field in Django.

Aman Khan
1 min readAug 18, 2022

Hi, Dev’s👏, Let’s see How to style field errors in Django?

In the following way we can access field errors:

{{field.errors}} → It outputs <ul class=”errorlist”> containing any validation errors corresponding to this field. You can customize the presentation of the errors with a

{% for error in filed.errors %} loop.

In this case, each object in the loop is a string containing the error message.

Example: {{form.name.errors}}

<ul class="errorlist">
<li> Enter your name</li>
</ul>

{{form.non_field_errors }} → This should be at the top of the form and the template lookup for errors on each field.

Example:

<ul class="errorlist nonfield">
<li> Generic validation error</li>
</ul>

Form:

from django import formclass StudentRegistration(forms.Form):
name = forms.CharField(error_messages={'required':'Enter your name'})

HTML

<form>
{% csrf_token %}
{{form.non_field_errors}}
{% for field in form %}
<div>
{{field.label_tag}} {{field}} {{field.errors}}
{% endfor %}
</form>

CSS

.errorlist{
styles you want to apply
}

In the Next Article we are going to see Model Form😁.

So Stay Tuned, I will see you in the next one.

Thanks for Following and Claps😋

Link to Day 33

Link to Starter Page

--

--

Aman Khan

Blockchain and Web3 Engineer Crafting compelling stories at the intersection of tech, business, and culture.