我正在尝试构建一个博客应用程序,问题是当我在模板中使用标签“truncatewords_html”来截断长度超过指定字数的帖子时,我需要通过一些标题链接到完整的帖子,例如“阅读更多...” ' 截断后。所以我应该知道帖子是否被截断。
P.S.:这是解决问题的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 %}
这相当复杂,但 django 有一些奇怪的角落。基本上我认为如果在 x 和 x+1 个单词处截断字符串长度是否相同,则字符串尚未被截断...
{% ifnotequal post.body|truncatewords_html:30|length post.body|truncatewords_html:31|length %}
<a href="#">read more...</a>
{% endifnotequal %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)