[Django]-Style Django form error message

5👍

Check the class name in the source code browser.
You can try to change the class name like that:

class MyForm(forms.Form):
    error_css_class = "error"
👤adrien

11👍

Just add this CSS to change error message color:
.errorlist
{
color: red;
}

or if you want to change field border color then
.error input, .error select {
border: 2px red solid;
}

6👍

I know this is an old question but I needed it as a beginner.

You can extend the ErrorList class in the

django.forms.utils

to style it as you wish.
In my case I wanted it as divs and Bootstrap alerts.

So I did this in forms.py

class DivErrorList(ErrorList):
    def __str__(self):
        return self.as_divs()

    def as_divs(self):
        if not self:
            return ''
        return '<div class="errorlist">%s</div>' % ''.join(['<div class="error alert alert-danger mt-1">%s</div>' % e for e in self])

and in the views.py

form = AddToCartForm(error_class=DivErrorList)

see the documentaion: customizing-the-error-list-format

5👍

To change the style of the error_message:

1st: Inspect the element of the error_message then copy its class

2nd: Add the style you want …

In my case…. the class of the error_message is help-block

<style>
.help-block{color:red;
            font-weight:bold;
}
</style>

JUST DON’T FORGET TO PUT IT INSIDE YOUR {% block content %} or {% block body %} on your html if you extended the base.html or else… it wont work

3rd: That’s All 🙂

Leave a comment