Python 如何获取小部件select的值?
请帮助选择小部件的选定值 model.py:Python 如何获取小部件select的值?,python,django,Python,Django,请帮助选择小部件的选定值 model.py: class UserProfile(User): CHOICES_teacher = ( ('0', 'qwe', ), ('1', 'asd',), ('2', 'zxc', ), ) teacher = models.CharField( 'teacher', max_length=30, ch
class UserProfile(User):
CHOICES_teacher = (
('0', 'qwe', ),
('1', 'asd',),
('2', 'zxc', ),
)
teacher = models.CharField(
'teacher',
max_length=30,
choices=CHOICES_teacher,
blank=True,
null=True,
)
forms.py:
class DrumDataForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = (
'teacher',
)
template.html:
{% if entries_user_profile.teacher %}
<div class="teacher">{{ entries_user_profile.teacher }}</div>
{% endif %}
{%if条目\用户\配置文件.teacher%}
{{entries\u user\u profile.teacher}}
{%endif%}
问题是屏幕显示0、1或2。但我需要在屏幕qwe或asd或zxc上从以下位置查看它们:
{{ entries_user_profile.get_teacher_display }}
对于已设置的每个字段,对象将有一个get\u FOO\u display()方法,其中FOO是字段的名称。此方法返回字段的“人类可读”值
因此,在您的示例中,您可以使用以下方法访问字段的“人类可读”值:
{{entries\u user\u profile.get\u teacher\u display}
使用{{{entries\u user\u profile.teacher.1}