Hi I am working through the Django book and am up to chapter 7. I got the end and was wondering if someone could explain this piece of code for me.
<div class="field{% if form.message.errors %} errors{% endif %}">
{% if form.message.errors %}
<ul>
{% for error in form.message.errors %}
<li><strong>{{ error }}</strong></li>
{% endfor %}
</ul>
{% endif %}
<label for="id_message">Message:</label>
{{ form.message }}
<div>
I don’t understand why you need the first part:
<div class="field{% if form.message.errors %} errors{% endif %}">
having just:
<div class="field">
seems to do the same thing.
Thanks.
When you got an error in your form, the attribute
form.message.errorswill be not empty. So, if it is not empty, the value'errors'will be outputted to your html, resulting in:It could then be used with a CSS file to display the error message with a special formatting.