Html 如果复选框为true或false,如何更新
模板Html 如果复选框为true或false,如何更新,html,django,Html,Django,模板 {% for me in student %} <input type='checkbox' name="check[0]" value="1" {% if me.Asthma %} checked="check" {% endif %} /> Ashtma </td></tr><tr valign='top'><td> <input type='checkbox' name="check[0]" value="
{% for me in student %}
<input type='checkbox' name="check[0]" value="1" {% if me.Asthma %} checked="check" {% endif %} /> Ashtma
</td></tr><tr valign='top'><td>
<input type='checkbox' name="check[0]" value="1" {% if me.CongenitalAnomalies %}checked="check" {% endif %} /> Congenital Anomalies
</td></tr><tr valign='top'><td>
<input type='checkbox' name="check[0]" value="1" {% if me.ContactLenses %}checked="check" {% endif %} /> Contact Lenses
</td></tr><tr valign='top'><td>
{% endfor %}
型号
asthma = request.POST.get('check[0]')
congenitalAnomalies = request.POST.get('check[0]')
Contact = request.POST.get('check[0]')
update.Asthma=asthma
update.CongenitalAnomalies=congenitalAnomalies
update.ContactLenses = Contact
Asthma=models.BooleanField(null=True, blank=True)
CongenitalAnomalies=models.BooleanField(null=True,blank=True)
ContactLenses=models.BooleanField(null=True,blank=True)
如果我不检查所有内容,但检查一个自动检查所有内容,则可以正常工作,请更正我的代码,为模板中的
元素使用唯一的名称
属性:
{% for me in student %}
<input type='checkbox' name="asthma" value="1" {% if me.Asthma %} checked="check" {% endif %} /> Ashtma
</td></tr><tr valign='top'><td>
<input type='checkbox' name="congenital_anomalies" value="1" {% if me.CongenitalAnomalies %}checked="check" {% endif %} /> Congenital Anomalies
</td></tr><tr valign='top'><td>
<input type='checkbox' name="contact-lenses" value="1" {% if me.ContactLenses %}checked="check" {% endif %} /> Contact Lenses
</td></tr><tr valign='top'><td>
{% endfor %}
请注意,只有在视图中只更新一个对象时,此操作才会起作用。如果视图中的代码段实际上处于循环中,则必须将对象的id添加到名称中
<input type='checkbox' name="asthma-{me.id}" value="1" {% if me.Asthma %} checked="check" {% endif %}
for update in students:
asthma = request.POST.get(f'asthma-{update.id}')
...
很抱歉,您的问题不清楚您是否遇到了一些错误?如果是,请发布错误回溯。您需要提供更多详细信息,如什么是更新
?为视图和模型添加更多详细信息。错误是当选中一个并单击“更新”按钮时,结果是“自动全部选中”,但您对每个视图和模型使用了相同的名称和值。很明显,他们都会做所有的事。我该怎么办?你能给我举个例子吗?
<input type='checkbox' name="asthma-{me.id}" value="1" {% if me.Asthma %} checked="check" {% endif %}
for update in students:
asthma = request.POST.get(f'asthma-{update.id}')
...