没有摘录时显示消息 - Django 模板
Show message when there's no excerpt - Django templates
我在 Django 模板上有这个字段:
<p class="border_dotted_bottom">
{{ expert.description|slice:":300" }}
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>
如果此对象(用户)没有 'decription'(文本字段),它会显示单词 'None',我需要删除它,也许如果他没有 'description' 然后显示一个简单的文本然后 "read more"
到目前为止,我试过这个:
<p class="border_dotted_bottom">
{{ % if expert.description >= 1 %}}
{{ expert.description|slice:":300" }}
{{% else %}}
Éste usuario por el momento no tiene descripción
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>
但它不起作用,我认为这只是一个错字,或者可能与我在这里使用的条件有关...
有人可以解释一下吗?
提前致谢!
您的问题出在 if/else
标签上。你有这个:
{{ % if ... %}}
...
{{% else %}}
...
首先,您需要用 {% %}
包围 if/else
,而不是 {{% %}}
。其次,你没有endif
。 if/else
块应该如下所示:
{% if ... %}
...
{% else %}
...
{% endif %}
因此,您想要的块看起来像这样:
<p class="border_dotted_bottom">
{% if expert.description >= 1 %}
{{ expert.description|slice:":300" }}
{% else %}
Éste usuario por el momento no tiene descripción
{% endif %}
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>
也就是说,你应该能够使用 Django 的内置 default
tag or default_if_none
tag 来简化这个,这取决于你是否想要在 expert.description
等于 ''
时给出默认值/None
或仅 None
:
<p class="border_dotted_bottom">
{{ expert.description|default:"Éste usuario por el momento no tiene descripción"|slice:":300" }}
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>
我在 Django 模板上有这个字段:
<p class="border_dotted_bottom">
{{ expert.description|slice:":300" }}
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>
如果此对象(用户)没有 'decription'(文本字段),它会显示单词 'None',我需要删除它,也许如果他没有 'description' 然后显示一个简单的文本然后 "read more"
到目前为止,我试过这个:
<p class="border_dotted_bottom">
{{ % if expert.description >= 1 %}}
{{ expert.description|slice:":300" }}
{{% else %}}
Éste usuario por el momento no tiene descripción
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>
但它不起作用,我认为这只是一个错字,或者可能与我在这里使用的条件有关...
有人可以解释一下吗?
提前致谢!
您的问题出在 if/else
标签上。你有这个:
{{ % if ... %}}
...
{{% else %}}
...
首先,您需要用 {% %}
包围 if/else
,而不是 {{% %}}
。其次,你没有endif
。 if/else
块应该如下所示:
{% if ... %}
...
{% else %}
...
{% endif %}
因此,您想要的块看起来像这样:
<p class="border_dotted_bottom">
{% if expert.description >= 1 %}
{{ expert.description|slice:":300" }}
{% else %}
Éste usuario por el momento no tiene descripción
{% endif %}
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>
也就是说,你应该能够使用 Django 的内置 default
tag or default_if_none
tag 来简化这个,这取决于你是否想要在 expert.description
等于 ''
时给出默认值/None
或仅 None
:
<p class="border_dotted_bottom">
{{ expert.description|default:"Éste usuario por el momento no tiene descripción"|slice:":300" }}
<a href="{% url 'profile' expert.username %}">{% trans "read more" %}</a>....
</p>