【问题标题】:How do I change the rendering properties of a django choice field?如何更改 django 选择字段的呈现属性?
【发布时间】:2020-07-24 14:17:40
【问题描述】:

以下代码根据用户的响应将“US”或“CA”呈现给模板。如何让它呈现相应的“美国”或“加拿大”值而不是两个字符代码?

来自:models.py

CANADA = 'CA'
UNITED_STATES = 'US'
COUNTRY_CHOICES = [
    (CANADA, 'Canada'),
    (UNITED_STATES, 'United States'),
]
address_country = models.CharField(
    max_length=2,
    choices=COUNTRY_CHOICES,
    default=UNITED_STATES,
    null=True,
)

来自:forms.py

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = ['address_country',]
        labels = {'address_country': 'Country',}

模板

Country: {{address.address_country}}

【问题讨论】:

    标签: python django django-models django-forms django-templates


    【解决方案1】:

    template试试这个

    {{address.get_address_country_display}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 2021-12-19
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多