我有CharField使用model_utils.Choices模型:与model_utils.Choices自定义模板
from model_utils import Choices
class UserTestStatistics(models.Model):
TEST_STATUSES = Choices(
('NR', 'not_readed'),('NP', 'not_passed'),
('FL', 'failed'), ('PD', 'passed'),
)
status = models.CharField(choices=TEST_STATUSES, max_length=2)
在模板我想这取决于状态字段值添加自定义CSS类。我试过这个:
{% if lecture.status == 'NP' %}
label-warning
{% endif %}
这没有工作。然后我试过这个:
context['statuses'] = UserTestStatistics.TEST_STATUSES
{% elif lecture.status == statuses.not_passed %}
label-warning
{% endif %}
它也失败了。这里是为什么:
>>> s = UserTestStatistics.objects.get(lecture=l)
>>> type(s.status)
<type 'unicode'>
>>> type(UserTestStatistics.TEST_STATUSES.passed)
<type 'str'>
快速和肮脏的解决办法是添加自定义模板标签,将转换既没有代码,然后比较,但对我来说,它看起来像我错了somwhere。
任何人都可以请建议更漂亮的东西?
我曾预料你尝试过的第一件事。 “{{lecture.status}}”的输出是什么? –
你忘了Unicode的模型中的 – catherine
有趣的事情:我一直在想这几天,却发现那里我错在两分钟后,我发表的问题。 Unicode方法与问题没有什么共同之处,它只是不同的模式。感谢大家。 – Melevir