1👍
✅
What you see in the browser is not what field.label_tag
really is.
Actually field.label_tag
is something like this (you can look at the HTML source):
<label for="id_password2">Password (Again):</label>
To quote a great man (and Django documentation):
{{ field.label_tag }} The field’s label wrapped in the appropriate
HTML tag.
This code should work:
{% if field.label_tag == '<label for="id_password2">Password (Again):</label>' %}
etc
Now, obviously, nobody wants to write code like this. Strings with the HTML code? Come on, Nigel, you’re better than this!
Here is the better way:
{% if field.name == 'password2' %}
etc
Actually, I think there is even a better way to handle form errors. You can read the documentation here.
Source:stackexchange.com