我在创造一个“字段.选择在django中定义项目可用的优先级状态。当我试图显示一个项目时,当我想显示/使用人类可读的名称时,我会被常量(CAP)卡住。在
例如,具有优先级的项目(MAITRISE,u'Maitrisé”)将显示为MAI(常量MAITRISE的值),而不是“Maitrisé”。在
我该怎么改变呢?如何选择要使用tuplue(MAITRISE,u'Maitrisé)中的哪个元素?在
这是我的字段选择(我添加或更改项目的优先级状态)
priority = models.CharField(max_length = 50,choices=PRIORITY_CHOICE,default=PRIORITAIRE)
MAITRISE = 'MAI'
IMPORTANT = 'IMP'
PRIORITAIRE = 'PRIO'
PRIORITY_CHOICE = (
(MAITRISE, u'Maitrisé'),
(IMPORTANT, u'Important'),
(PRIORITAIRE, u'Prioritaire'),
)
风景
^{pr2}$模板
<div class="row">
<!-- x,y tuple is like MAITRISE, u"maitrisé" avec MAITRISE = MAI -->
{% for x,y in priority_scale %}
<!-- if the priority status of the item match the current loop in priority scale list => button should be red -->
{% if y == item.priority %}
<div class="col-xs-2">
<a href=""><button class="btn btn-danger">{{y}}</button></a>
</div>
{% else %}
<div class="col-xs-2">
<a href=" {% url 'memory:change_priority' mem_id=item.id new_priority=x %}">
<button class="btn btn-primary">{{y}}</button></a>
</div>
{% endif %}
{% endfor %}
</div><br>
当然这是基本技能,但是文档在这方面有点欠缺
对于具有} 。对于你的情况,应该是
choices
的字段,调用instance.fieldname
将返回该字段的实际值。如果您希望从choices元组获得字段的详细值,则需要调用^{item.get_priority_display()
。但是请记住,不能在Django模板中调用那样的函数。所以调用item.get_priority_display
将得到显示值。在相关问题 更多 >
编程相关推荐