[Django]-Initial value in django form dropdown is not empty?

3👍

Ok Solution was simple. In my from file I have added

from django.db.models.fields import BLANK_CHOICE_DASH

and updated the form

class PaymentRangeForm(forms.Form):

    start_date = forms.DateField(widget=forms.TextInput(attrs={'type': 'date'} ))
    end_date = forms.DateField(widget=forms.TextInput(attrs={'type': 'date'} ))
    building = forms.ChoiceField(choices=BLANK_CHOICE_DASH + list(Building.objects.values_list('id', 'name')), required=False)

0👍

load crispy_forms_tags top of the template.

{% load crispy_forms_tags %}

 <form method="POST"  class="form" action="" method="get">
                {% csrf_token %}
                {{ form|crispy}} 
                <br>
                <button type="submit" class="btn btn-primary btn-primary">Search</button>
</form>

-1👍

According to Django, perhaps you could try it out without the .all() as below

choices=Building.objects.values_list(‘id’, ‘name’)

👤Zhong

Leave a comment