django表单:如何在模板中签出复选框状态 [英] django form: how to check out checkbox state in template
问题描述
我有一个带有复选框的表单,该表单运行良好,但是当用户提交带有错误的表单并且选中了我的复选框时,我需要更改保存该复选框的div类。因此,我正在模板中使用 {{form.mycheckbox.data}}
进行实验,它在页面加载时显示 False 。现在,当用户单击复选框且表单有错误时,它会在 on 上显示。所以我尝试了:
I have a form with checkboxes that works fine, but when the user submits the form with errors and my checkbox is checked I need to change div class which holds the checkbox. So I was experimenting with {{ form.mycheckbox.data }}
in the template and it says False on page load. Now when user is click on a checkbox and the form has errors, it says on. So I tried:
{{如果form.mycheckbox.data == True}}
不起作用
{{如果form.mycheckbox.data!= False}}
不起作用
{{如果form.mycheckbox.data =='on'}}
不起作用
{{如果form.mycheckbox.data == on}}
不起作用
推荐答案
使用 {%,如果form.mycheckbox.value%}
。如果选中此框,则该值为true。对于相反的行为,请使用 {%,如果不是form.mycheckbox.value%}
。
Use {% if form.mycheckbox.value %}
. This will evaluate to true if the box is checked. For the opposite behavior, use {% if not form.mycheckbox.value %}
.
请注意,语法为 {%if ...%}
,而不是 {{if ...}}
。括号放在命令中,括号放在输出变量中。
Note the syntax is {% if ... %}
, not {{ if ...}}
. Percent-brackets are for commands, double-brackets are for outputting variables.
这篇关于django表单:如何在模板中签出复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!