use*_*641 6 django django-templates django-models django-forms django-views
的index.html
<td>{% if place or other_place or place_description%}{{ place}} {{ other_place}} {{place_description}}</td>
Run Code Online (Sandbox Code Playgroud)
这是在模板中显示所有数据.如果字符串长度超过80,我想截断它.
条件是,1.如果地方变量有超过80个字符,它应截断它们,不需要显示另外两个变量,如other_place和place_description.
2.如果放置变量和other_place变量超过80个字符,在这种情况下它应该从place_variable截断不需要显示place_description变量.
3.如果所有这三个都是他们的,而第80个字符是由place_description制作的,则需要截断他们的.
所有字段都不是必填字段,因此无论显示哪个字段,它都应该只显示80个字符.
需要帮助才能做到这一点.
谢谢
kar*_*ikr 12
你可以使用slice for pre-django 1.4:
{% if place or other_place or place_description%}
{% with place|add:other_place|add:place_description as pl %}
{% if pl|length > 80 %}
{{pl|slice:80}}...
{% else %}
{{pl }}
{% endif %}
{% endwith %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是django 1.4或更高版本,
你可以使用 truncatechars
{% if place or other_place or place_description%}
{% with place|add:other_place|add:place_description as pl %}
{{pl|truncatechars:80}}
{% endwith %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12319 次 |
| 最近记录: |