Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-countries cannot get country name

Tags:

django

Based on the django-countries documentation,

>>> person.country
Country(code='NZ')
>>> person.country.name
u'New Zealand'

should be a way to retrieve the unicode for the country name. However, when I try it i get

>>> obj.country
Country(code='AX')
>>> obj.country.name
<django.utils.functional.__proxy__ object at 0x91b81ac>

I checkout out countries.py and saw that the choices look like this:

('AX', ugettext_lazy('\xc5land Islands'))

Even using print object.country.name prints the same object. Why is it not working?

edit: Sorry, i just put the name object as a sample :p

like image 593
Kia Avatar asked Jun 30 '11 15:06

Kia


People also ask

What is Django countries?

A Django application that provides country choices for use with forms, flag icons static files, and a country field for models.

How do I enter a country code in Django?

In Django, we have a third-party package called 'django-countries' that provides the country field. In this article, let's see how to use django-countries to add a Country field in Django. First, create a Django project and an app. Add the app in INSTALLED_APPS and set up urls.


1 Answers

Call unicode() on it

https://docs.djangoproject.com/en/1.3/ref/unicode/#translated-strings

from django.utils.translation import ugettext_lazy

u = ugettext_lazy('hello')
print u
# out: <django.utils.functional.__proxy__ object at 0x158edd0>

print unicode(u)
# out: u'hello'

Normally not an issue if rendered in a template.

like image 134
Yuji 'Tomita' Tomita Avatar answered Sep 17 '22 18:09

Yuji 'Tomita' Tomita