gpt4 book ai didi

Django form.MultipleChoiceField 。自定义模板中的复选框

转载 作者:行者123 更新时间:2023-12-02 05:36:35 34 4
gpt4 key购买 nike

我有这个表格:

class PasswordForm(forms.Form):

CHOICES=[('uppercase','Uppercase'),
('lowercase','Lowercase'),
('numbers','Numbers'),]

password_length = forms.ChoiceField(choices=[(x, x) for x in range(1, 32)],)
options = forms.MultipleChoiceField(
widget=forms.CheckboxSelectMultiple, choices=CHOICES,)

如何在模板中自定义我的复选框字段?
我知道如何使用其他字段(例如 forms.Charfield() 或我的 password_length)来做到这一点,只需

<form action="" method="post">
{{form.some_field}}
</form>

但它不适用于我的MultipleChoiceField,我尝试了很多东西,例如
{{form.uppercase}}{{form.options.choices.uppercase}} 并尝试了 {% for %} 循环。< br/>它只是返回任何内容,并且我没有通过浏览器检查器在 html 中看到它。

最佳答案

 <!-- With for-cycle: -->
{% for field in form %}
{% for choice_id, choice_label in field.field.choices %}
{{choice_id}}
{{choice_label}}
{% endfor %}
{% endfor %}

<!-- And my end version, where I edited only certain field. -->
{% for field in form %}
{% if field.name == "your-field-name-here"%}
<p><strong>{{field.name}}: </strong></p>
{% for choice_id, choice_label in field.field.choices %}
<input type="checkbox" name="category" value="{{choice_id}}" style="display:inline;">{{choice_label}}
{% endfor %}
{% else %}
<p>{{ field.errors }}<label style="display:table-cell;">{{field.name}}: </label>{{ field }}</p>
{% endif %}
{% endfor %}

<!-- If you want to edit all fields with options: {% if field.choices %} -->

关于Django form.MultipleChoiceField 。自定义模板中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34606834/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com