我正在尝试构建一个博客应用程序,问题是当我在我的模板中使用标签'truncatewords_html'来截断比指定数量的单词更长的帖子时,我需要通过某些标题链接到完成帖子,例如"阅读更多... '截断后.所以我应该知道帖子被截断了.
PS:这是解决问题的pythonic方法吗?
{% ifequal post.body|length post.body|truncatewords_html:max_words|length %}
{{ post.body|safe }}
{% else %}
{{ post.body|truncatewords_html:max_words|safe }}<a href="{{ post.url}}">read more</a>
{% endifequal %}
Run Code Online (Sandbox Code Playgroud)
这是非常令人费解,但django有一些奇怪的角落.基本上我想如果你在x和x + 1字处截断字符串长度是相同的那么字符串没有被截断...
{% ifnotequal post.body|truncatewords_html:30|length post.body|truncatewords_html:31|length %}
<a href="#">read more...</a>
{% endifnotequal %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1470 次 |
| 最近记录: |