Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Forms Forms Using and Creating Validators

Aimn Blbol
Aimn Blbol
10,352 Points

message error

Hi How did the message from this line go to the top of the page:

honypot = forms.CharField( required = False, widget = forms.HiddenInput, validators = [validators.MaxLengthValidator(0)])

In my base.html, I am trying to catch messages but I am not getting the message from the validator. This is what I have in base.html:

{% if messages %}
  {% for message in messages %}
    <div class="container-fluid">
      {% if message.tags == 'error' %}
        <div class="alert alert-danger alert-dismissible" role="alert">
      {% elif message.tags == 'success' %}
        <div class="alert alert-success alert-dismissible" role="alert">
      {% endif %}
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          {{ message }}
        </div>
    </div>
  {% endfor %}
{% endif %}

1 Answer

Haydar Al-Rikabi
Haydar Al-Rikabi
5,971 Points

By default, each validation error appears on top of its related form field.

In order to style this error, use the following format in your template file:

{% for field in form %} # 'form' is the context data passed from your views.py
    {% if field.errors %}
        {% for error in field.errors %}
            <div class="your_css_style">
                {{ error }}
            </div>
        {% endfor %}
    {% endif %}
{% endfor %}