【问题标题】:How to use MultipleChoiceField in django-widget-tweaks?如何在 django-widget-tweaks 中使用 MultipleChoiceField?
【发布时间】: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


    【解决方案1】:

    {% empty %} 子句仅在给定数组为空或不存在时才会显示文本。在这种情况下,即使选项为空,表单也将始终包含字段。 您应该尝试直接检查选项并仅在表单不为空时显示。

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 1970-01-01
      • 2018-05-04
      • 2016-09-19
      • 2018-12-09
      • 2019-08-28
      • 1970-01-01
      • 2016-06-15
      • 2018-07-30
      相关资源
      最近更新 更多