【3.7】实现最近5篇文章列表

1.从数据库中检索5篇最新文章

1 top5_article_list = Article.objects.all().order_by('-publish_date')[:5]

2.修改视图函数

1 return render(request, 'blog/index.html',
2                   {
3                       'article_list': page_article_list,
4                       'page_num': range(1, page_num + 1),
5                       'curr_page': page,
6                       'next_page': next_page,
7                       'previous_page': previous_page,
8                       'top5_article_list': top5_article_list
9                   })

3.修改HTML页面

1 <h2>最新文章</h2>
2                 {% for article in top5_article_list %}
3                 <h4><a href="/blog/detail/{{ article.article_id }}">{{ article.title }}</a></h4>
4                 {% endfor %}
原文地址:https://www.cnblogs.com/zydeboke/p/11452257.html