Django templates: verbose version of a choice

前端 未结 8 1146
野性不改
野性不改 2020-11-30 18:16

I have a model:

from django.db import models

CHOICES = (
    (\'s\', \'Glorious spam\'),
    (\'e\', \'Fabulous eggs\'),
)

class MealOrder(models.Model):
          


        
8条回答
  •  悲&欢浪女
    2020-11-30 18:39

    The best solution for your problem is to use helper functions. If the choices are stored in the variable CHOICES and the model field storing the selected choice is 'choices' then you can directly use

     {{ x.get_choices_display }}
    

    in your template. Here, x is the model instance. Hope it helps.

提交回复
热议问题