{%if%}标记用于显示删除线

时间:2016-06-04 17:02:41

标签: django-templates

让我们有一个带有模型Person的DetailView。我们假设此Person实例的year_of_birth = None。

Django模板语言能组织这样的东西吗?

{% with "---" as strikethrough %}

<p>Year of birth:  {% object.year_of_birth or strikethrough %}

{% endwith %}

我尝试过大括号。无论如何我得到这样的东西:

Exception Type: TemplateSyntaxError
Exception Value:    
Invalid block tag on line 9: '{strikethrough}', expected 'endwith'. Did you forget to register or load this tag?

那么,在这种情况下使用'或'的想法是否可行,或者我必须使用{%if%}标签?

1 个答案:

答案 0 :(得分:0)

您可以使用defaultdefault_if_none模板标记:

...
<p>Year of birth: {% object.year_of_birth|default:strikethrough %}</p>
...

另请查看firstof以获取具有两个以上选项的逻辑。