Django


1211 浏览 6 years, 1 month

7.3 模板显示

版权声明: 转载请注明出处 http://www.codingsoho.com/

django模板中取值

字典

{% for key,value in dicts.items %}
    <li>{{key}} = {{ value }}</li>
{% endfor %}

元组

{% for item in tuple_lst %}    
     {{ item.0 }} {{ item.1}} {{ item.2 }}    
{% endfor %}

如何显示choice的值

choice定义如下

    daily_insepction_correction_status = (
        ('completed', _('Completed')),
        ('uncompleted', _('Uncompleted')),
    )

调用下面函数,force_text会将元素转换成text

class Model(six.with_metaclass(ModelBase)):
    def _get_FIELD_display(self, field):
        value = getattr(self, field.attname)
        return force_text(dict(field.flatchoices).get(value, value), strings_only=True)