开发者

Localizing CharFields and ModelForms with choices in Django

I have a Model and ModelForm like:

from django.utils.translation import ugettext_lazy as _

class Item(models.Model):

    category = models.CharField(
        max_length=256,
        choices=(
            ('car', _("Cars")),
            ('computers', _("Computers")),
            ('furry animal', _("Furry or large animals")),
            ))


class ItemForm(ModelForm):
    class Meta:
        model = Item

I have properly catalogued, translated and compiled the strings, and {% trans "Cars" %} shows the translated text as expected.

Now if I pass instances of ItemForm and Item to a template, and use them as {{ form }} and {{ item.category }}, am I correct to assume that they will not by default show the localized choices?

What would be the cleanest and simplest way of开发者_如何学运维 having them localized?


Have you tried this yourself?

I'm pretty sure they would be localized. At least {{ form }}, whereas you would want to use {{ item.get_category_display }} to get the display value for the category field (i.e. "Furry or large animals"/"Pelzige oder grosse Tiere" (localized) rather than "furry animal" (internal string)).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜