Changing how forms.ValidationError is displayed in django

0

Good evening, is it possible to change how ValidationError is displayed? I noticed that django creates a <ul> tag and inside it a <li> tag with the error. Can I change the way this is done? And if I wanted to change the class of these elements, how would I?

I'm trying to do this in django authentication, I took a look at the source code and django.contrib.auth.forms in the form AuthenticationForm (only part related to displaying the error) looks like this:

 error_messages = {
        'invalid_login': _(
            "Please enter a correct %(username)s and password. Note that both "
            "fields may be case-sensitive."
        ),
        'inactive': _("This account is inactive."),

 def clean(self):
    username = self.cleaned_data.get('username')
    password = self.cleaned_data.get('password')

    if username is not None and password:
        self.user_cache = authenticate(self.request, username=username, password=password)
        if self.user_cache is None:
            raise self.get_invalid_login_error()
        else:
            self.confirm_login_allowed(self.user_cache)

    return self.cleaned_data


 def get_invalid_login_error(self):
        return forms.ValidationError(
            self.error_messages['invalid_login'],
            code='invalid_login',
            params={'username': self.username_field.verbose_name},
        )
    
asked by anonymous 10.04.2018 / 01:06

1 answer

0

You can access the list of errors in your template. For example, to create h1 tags with the errors of a name field of the form:

{% if form.name.errors %}
    {% for error in form.name.errors %}
        <h1 class="header-erro">{{ error }}</h1>
    {% endfor %}
{% endif %}
    
10.04.2018 / 02:44