views:

567

answers:

5

I have a form for address information. One of the fields is for the address country. Currently this is just a textbox. I would like a drop down list (of ISO 3166 countries) for this. I'm a django newbie so I haven't even used a Django Select widget yet. What is a good way to do this?

Hard-code the choices in a file somewhere? Put them in the database? In the template?

+6  A: 

Check out "choices" parameter of a CharField.

You might also want to take a look at django-countries.

bebraw
+1 django-countries
Török Gábor
+3  A: 

check this, here is a list of all countries based on the ISO 3166 programmed in python and use choices option in django model.

eos87
+2  A: 

Ended up using the below snippet which basically defines a tuple of tuples of two character country codes. Additionally it defines a custom field type called CountryField and defaults the choices parameter to the above defined tuple. This automatically gets rendered as a drop down list.

http://djangosnippets.org/snippets/494/

User
django-countries provides exactly the same. Its a reusable component.
Török Gábor
A: 

Django Countries

from django_countries.countries import COUNTRIES

class CountryForm(forms.Form):
      country= forms.ChoiceField(COUNTRIES)
Asinox
django-countries has a CountryField class now.
Wahnfrieden
cool with CountryField
Asinox
+1  A: 

Django Countries

from django_countries import CountryField

class Foo(models.Model):
    country = CountryField()
Wahnfrieden
great project try this one
soField