0

I have form with MultipleChoiceField field which has dynamic list for choices. With the help of this form users can select data and add them to database.

Sometimes dynamic list can be empty []. So I want to show message in template when its empty but next code didnt show me message. I use django-widget_tweaks application in my template. Where is my mistake?

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>
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

1 Answers1

1

The {% empty %} clause will display the text only when the given array is empty or it doesn't exist. In this case the form will always have fields even if the choices are empty. You should try checking directly with the choices and show the form only when its not empty.

mking
  • 129
  • 6