[Django]-Empty ChoiceField selection in Django forms

7👍

Just add:

(u'', u'Select Year')  # First element will be the `value` attribute.

So:

choices = [(u'', u'Select Year')]
choices.extend([(unicode(year), unicode(year)) for year in range(1970, 2015)])
graduation = forms.ChoiceField(choices=choices)

BTW, I used unicode because you were using str, but in practice a year field should probably be an integer, since all years are integers.

1👍

#city choices
city_list=city.objects.all()
choices_city = ([('----select----','----select a city----')])
choices_city.extend([(x,x) for x in city_list])
city =forms.CharField(widget=forms.Select(choices=choices_city))

I used it for django 1.8

Leave a comment