django 使用内建过滤器实现文章摘要效果

django 使用内建过滤器实现文章摘要效果

前端html代码

 1 <div class="list-group">
 2         {% if articles %}
 3         {% for article in articles.object_list %}
 4         <li class="list-group-item">
 5             <h3>
 6             <a href="{% url 'blog:detail' article.id %}" >
 7                 {{ article.title }} <br /></a>
 8             </h3>
 9                 <!--显示摘要-->
10                 {{ article.content|truncatechars_html:100 }}
11                 <!--ending 摘要-->
12             <a href="{% url 'blog:detail' article.id %}" >
13                 阅读全文</a>
14             <br />
15             {{ article.author }}
16         {% endfor %}
17         {% else %}
18                 <p>No articles are available.</p>
19         {% endif %}
20         </li>
21     </div>

使用truncatechar_html选择文章内容的前一百个字符,不包括html标签。

文档地址:http://python.usyiyi.cn/django_182/ref/templates/builtins.html

 

posted @ 2016-04-24 22:00  hb91  阅读(664)  评论(0编辑  收藏  举报