楷模:
completed = models.BooleanField(_('Completed'))
模板:
{% if object.completed %}
<strong>{{ object.completed }}</strong>
{% endif %}
输出:
<strong>True</strong>
我需要的:
<strong>Completed</strong>
楷模:
completed = models.BooleanField(_('Completed'))
模板:
{% if object.completed %}
<strong>{{ object.completed }}</strong>
{% endif %}
输出:
<strong>True</strong>
我需要的:
<strong>Completed</strong>
另外,请查看yesno
模板过滤器。用法:
<strong>{{ object.completed|yesno:"Completed,Uncomplited" }}</strong>
或者:
<strong>{{ object.completed|yesno:"Completed," }}</strong>
另一方面,您始终可以制作自己的模板过滤器。例如,下一个返回一个verbose_name
指定字段:
foo_tags.py:
@register.filter()
def get_field_name(object, field):
verbose_name = object._meta.get_field(field).verbose_name
return verbose_name
模板.html:
{% if object.completed %}
<strong>{{ object|get_field_name:'completed' }}</strong>
{% endif %}
{% if object.completed %}<strong>Completed</strong>{% endif %}
您可以尝试将标签属性添加到表单字段:
completed = forms.BooleanField(label=mark_safe('<strong>Completed</strong>'))
当您使用时{{ form.completed.label }}
,您将拥有<label><strong>Completed</strong></label>
我认为您无法使用对象实例来获得它。如果您modelForm
在模板中有该模型,则可以使用{{form.completed.label}}
我会简单地使用:
{% if object.completed %}
<p><strong>Completed</stromg></p>
{% else %}
<p><strong>Not Completed</strong></p>
{% endif %}