如何在django的模板中使用Choice字段的值?
下面一段代码,
Model:
class Person(models.Model):
name = models.CharField(max_length=200)
CATEGORY_CHOICES = (
('M','Male'),
('F','Female'),
)
gender = models.CharField(max_length=200, choices = CATEGORY_CHOICES)
to_be_listed = models.BooleanField(default=True)
description = models.CharField(max_length=20000, blank=True)
views.py:
def index(request):
latest_person_list2 = Person.objects.filter(to_be_listed=True)
return object_list(request, template_name='polls/schol.html',
queryset=latest_person_list, paginate_by=5)
在模板里面, 当我调用 person.gender, 我得到的是 'M' 或 'F',而不是 'Male' 或 'Female'。 请问如何才能显示('Male' 或 'Female')呢?
Tony大本
11 years, 3 months ago
Answers
链接换成了: https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display
tsuyako
answered 11 years, 1 month ago