如何在django模板中引用选项列表?

4 django template-engine

我的django模型中有以下内容:

PRIORITY = (
    (1, 'Low'),
    (2, 'Normal'),
    (3, 'High'),
)
Run Code Online (Sandbox Code Playgroud)

显然,与此相关的条目是存储整数.在我的模板中,我想以人类可读的格式显示优先级.我究竟该如何做到这一点?

我的模板:

{% for x in items %}
{{ x }} (added on {{ x.create_date }})<br>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

{{ x.id }} 将是优先ID.

提前致谢.

Wog*_*gan 20

假设您在定义模型时正确设置了选项选项,Django会自动创建辅助函数以显示您的名称.有关详细信息,请参阅有关额外实例方法的文

如果您的模型实例是,x并且您的属性存储了优先级priority,那么在您的模板中,您将使用:

{{ x.get_priority_display }}
Run Code Online (Sandbox Code Playgroud)