【发布时间】:2017-09-28 16:41:30
【问题描述】:
我的表单带有MultipleChoiceField 字段,其中包含choices 的动态列表。借助此表单,用户可以选择数据并将其添加到数据库中。
有时动态列表可能为空[]。所以我想在模板为空但下一个代码没有显示消息时在模板中显示消息。我在我的模板中使用django-widget_tweaks 应用程序。我的错在哪里?
forms.py:
class RequirementForm(forms.ModelForm):
symbol = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
class Meta:
model = Requirement
fields = ('symbol',)
requirement_add.html:
{% load widget_tweaks %}
<form method="post" action="{% url 'project:requirement_add' project_code=project.code %}">
{% for field in form %}
{% render_field field class="form-control" %}
{% empty %}
<p>Form is empty!</p>
{% endfor %}
</form>
【问题讨论】:
标签: python django django-forms django-templates django-widget-tweaks