what is the best way to write a combo box in django?

django, html, python

Solution

If you have a static list of cities, you can create a combobox using `ChoiceField`:

from django import forms

class SelectCityForm(forms.Form):
    CITY_1 = 'city_1'
    CITY_2 = 'city_2'
    CITY_CHOICES = (
        (CITY_1, u"City 1"),
        (CITY_2, u"City 2")
    )
    cities = forms.ChoiceField(choices=CITY_CHOICES)

IF you are saving cities into the database, you can use `ModelChoiceField`:

class SelectCityForm(forms.Form):
    cities = forms.ModelChoiceField(queryset=City.objects.all())

Problem

I try making a combo box in django. I only found doing it with HTML. Is there a form based django code to do it? For example I want a combo box with cities that I can select and then choose and hit submit to send the city to another page. thanks

Original source