Django: Country drop down list?

怎甘沉沦 提交于 2019-12-03 06:57:38

问题


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?


回答1:


Check out "choices" parameter of a CharField.

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




回答2:


Django Countries

from django_countries.fields import CountryField

class Foo(models.Model):
    country = CountryField()



回答3:


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/




回答4:


Django Countries

from django_countries.countries import COUNTRIES

class CountryForm(forms.Form):
      country= forms.ChoiceField(COUNTRIES)



回答5:


Two low mantained projects:

http://code.google.com/p/django-countries/

https://github.com/strange/django-country-utils

Two snippets:

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

http://djangosnippets.org/snippets/1476/

For now, I am going for a snippet.




回答6:


Here is a nice library with countries (and not only): pycountry

Its main advantage is that it is a wrapper around Debian package pkg-isocodes (thus can updates automatically with it) compared to hard-coded countries in other solutions. It also has translations.

So if new country appears or existing countries will be merged together you do not need to change your code.

I found it useful to use this library and create a simple Django app with model Country for example

Then you can populate and keep up-to-date your 'country' table by means of custom django-admin command as described here: Writing custom django-admin commands




回答7:


As previous answers have stated, there is a CountryField in django-countries. This is a model field.

If a form field is needed in a plain form (not model form), in django-countries v3.x (definitely tested in 3.3) the following can be used:

from django_countries.data import COUNTRIES

class MyForm(forms.Form):
    country = forms.ChoiceField(sorted(COUNTRIES.items()))



回答8:


Put them in the database is a better way. Convenient to management.




回答9:


Link to package : django-countries
If looking for how to do it in forms :

$ pip install django-countries

>>> from django_countries.data import COUNTRIES

>>> Country = forms.ChoiceField(choices = sorted(COUNTRIES.items()))



回答10:


Here is the solution:

from django_countries.fields import CountryField

class Foo(TimeStampedModel):

    country = CountryField()



回答11:


I solved it by using multiple=True:

from django_countries.fields import CountryField    

class UserProfile(models.Model):
    countries = CountryField(multiple=True)

You can read more about it in the docs:

https://github.com/SmileyChris/django-countries



来源:https://stackoverflow.com/questions/2963930/django-country-drop-down-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!