python - Django 如何显示文章摘要?
黄舟
黄舟 2017-04-17 12:59:26
0
3
440

需要在blog主页显示文章的摘要, 即显示文章的前面一部分.

在django中, 我尝试过以下做法:

article|safe|truncatechars:100      // 截取文章的前100个单词

但问题是, 文章中为了排版可能带有html标记(实际上是使用了markdown语法), 如:

  who are you ?<p>I am python </p> 

如果不凑巧, 碰巧截取掉了某个html标记, 如变为:

  who are you ?<p>I am python            //少了个结束标志 '</p>'

那么整个页面都会被破坏.

怎么办呢? 或者, 有更好的方法实现吗?(最好不要用javascript来隐藏或显示)

谢谢大家.

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
大家讲道理

truncatewords_html or truncatechars_html (django 1.7 or above)

洪涛

1. You can try to add tags to the article manually, such as
In view.py, use object.values() and [0:blog.find('')] to intercept the content before the label.

2. Write the introduction of the article yourself.

伊谢尔伦

Just use markdown filter on the data after truncate

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template