如何发现内容被截断? [英] How to find that the content is truncated?

查看:84
本文介绍了如何发现内容被截断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建博客应用,问题是当我在模板中使用标签'truncatewords_html'截断长度超过指定单词数的帖子时,我需要链接以完成标题,例如阅读更多信息 ...'截断后。因此,我应该知道该职位是否已被截断。

I'm trying to build a blog app and the problem is when I use tag 'truncatewords_html' in my template to truncate posts longer than specified number of words, I need to link to complete post by some title like 'read more...' after truncation. So I should know that the post was truncated or not.

PS:这是解决问题的一种Python方法吗?

P.S.: Is this a pythonic way to solve the problem?

{% 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字词截断,则字符串长度是否相同,则该字符串并未被截断...

This is pretty convoluted but django has some weird corners. Basically I figure if the string length is the same if you truncate at x and x+1 words then the string has not been truncated...

{% ifnotequal post.body|truncatewords_html:30|length post.body|truncatewords_html:31|length %}
   <a href="#">read more...</a>
{% endifnotequal %}

这篇关于如何发现内容被截断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆