[Answered ]-How do I style and align errorlist from django forms?

1👍

According to the official documentation, you can pass an individual error class to the form instance on construction like so:

# forms.py

from django.forms.utils import ErrorList

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

     def as_divs(self):
         if not self: return ''
         return '<div class="your_css_class">%s</div>' % ''.join(['<div 
               class="your_css_class">%s</div>' % e for e in self])

Now in your view you could do s.th. like

# views.py

# Initiate form instance and pass the custom error_class
my_form = ContactForm(data, auto_id=False, error_class=DivErrorList)

...
# CSS

.your_css_class {
    font-size: 2em;
}
👤JSRB

Leave a comment