Reputation: 366
I need to add required
class to a div element to render the fields of AuthenticationForm
. But for a reason I can't understand, inside the template, form.password.required
does not evaluate to True:
<div class="field {% if form.password.required %}required{% endif %}">
{{ form.password.label_tag }}
{{ form.password }}
</div>
Problem is, when I instantiate a form manually
>>> from django.contrib.auth.forms import AuthenticationForm
>>> f = AuthenticationForm(None, {"password": "foo", "username":"bar"})
>>> f.is_valid()
>>> f.fields["password"].required # is true
Why isn't it true in the template ?
Upvotes: 1
Views: 159
Reputation: 32244
form.password
gives you a BoundField and not the password field itself, you need to use the field
attribute of that BoundField
to access the field and it's attributes
<div class="field {% if form.password.field.required %}required{% endif %}">
Upvotes: 1