Django 模板中的运算符计算
Operator calculations inside a Django template
在 Django 模板中我有这样一个短语:
<h4>{{ webinar.name or 'Not title' }}</h4>
导致此错误:
Could not parse the remainder: ' or 'Not title'' from 'webinar.name or 'Not title''
这是因为 Django 不喜欢在模板中计算运算符。 django-mathfilters 似乎没有 or
运算符。我也不喜欢使用 {% if ... %}
。因为这是一个 MWE。我遇到过其他我无法轻易解决的案例。
试试这个:
<h4>{{ webinar.name|default:'Not title' }}</h4>
Django 在模板中确实有运算符,但这行不通。
执行此操作并保持代码整洁的最简单方法是:
<h4>{% if webinar.name %}{{ webinar.name }}{% else %}No title{% endif %}</h4>
在 Django 模板中我有这样一个短语:
<h4>{{ webinar.name or 'Not title' }}</h4>
导致此错误:
Could not parse the remainder: ' or 'Not title'' from 'webinar.name or 'Not title''
这是因为 Django 不喜欢在模板中计算运算符。 django-mathfilters 似乎没有 or
运算符。我也不喜欢使用 {% if ... %}
。因为这是一个 MWE。我遇到过其他我无法轻易解决的案例。
试试这个:
<h4>{{ webinar.name|default:'Not title' }}</h4>
Django 在模板中确实有运算符,但这行不通。
执行此操作并保持代码整洁的最简单方法是:
<h4>{% if webinar.name %}{{ webinar.name }}{% else %}No title{% endif %}</h4>