[Django]-ModelForm and error_css_class

0👍

You can define your own error list class by inherting from django’s ErrorList. See the docs for details:

Note that you’ll have to override the method to output the full HTML and can’t just replace CSS class. You could call the base method and do a string replace on “class=\”errolist\”” and return the output.

👤ars

0👍

create custom class thath extends ErrorList

class CustomErrorList(ErrorList):
    def get_context(self):
        return {
            "errors": self,
            "error_class": "your css class",
        }

And add it in your form class:

class CustomForm(ModelForm):
def init(self, *args, **kwargs):
super().init(*args, **kwargs)
self.error_class = CustomErrorList

Leave a comment