Django AuthenticationForm必填字段

最大值

我需要将required添加到div元素以呈现的字段AuthenticationForm但是由于某种原因,我无法理解,在模板内部,form.password.required它的求值结果为True:

<div class="field {% if form.password.required %}required{% endif %}">
 {{ form.password.label_tag }}
 {{ form.password }}           
</div>

问题是,当我手动实例化表单时

>>> from django.contrib.auth.forms import AuthenticationForm
>>> f = AuthenticationForm(None, {"password": "foo", "username":"bar"})
>>> f.is_valid()
>>> f.fields["password"].required # is true

为什么在模板中不正确?

伊恩·谢尔文顿

form.password给你一个绑定列,而不是密码字段本身,你需要使用field的该属性BoundField访问该字段,它的属性

<div class="field {% if form.password.field.required %}required{% endif %}">

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章