Translate values dict-like in Django templates
In my Model I define choices along the lines of:
LANG_CHOICES = (
("Englisch", (
(u"en-gb", u"England"),
(u"en-us", u"United States of America"),
), )
T开发者_如何学JAVAhe field is defined as:
lang_source = models.CharField(max_length=5, choices=LANG_CHOICES, default="en-gb")
Naturally, in my template I'd want to display the human-readable value, i.e.
{{ object.lang_source }}
should not print "en-gb" (or the respective value) but rather "England".
What is the most elegant way to accomplish this? (Besides in the View importing a dict from the Model and manually translating the value.)
Try:
object.get_lang__source_display()
Documentation:
- http://www.djangoproject.com/documentation/models/choices/
- http://docs.djangoproject.com/en/1.1/ref/models/instances/#django.db.models.Model.get_FOO_display
http://www.djangoproject.com/documentation/models/choices/
{{ object.get_lang_source_display }}
should work.
Using two underscores in a row when defining field name is forbidden, because of the way django filters QuerySets: http://docs.djangoproject.com/en/dev/topics/db/models/#field-name-restrictions
精彩评论