1.项目:http://www.cnblogs.com/jasonhaven/p/7493422.html

2.任务描述:页面分页显示文章

3.源代码

后台:

from django.core.paginator import Paginator, InvalidPage, EmptyPage, PageNotAnInteger
from models import *
def getPage(request):
article_list = Article.objects.all()
paginator = Paginator(article_list, 2)
try:
page = int(request.GET.get('page', 1))
article_list = paginator.page(page)
except (EmptyPage, InvalidPage, PageNotAnInteger):
article_list = paginator.page(1)
return article_list

前台:

{% block left_content %}
{% include 'ad.html' %}<!--广告-->
<div class="topnews">
<h2>最新文章</h2>
{% for article in article_list %}
<div class="blogs">
<ul>
<h3><a href="{% url 'article' %}?id={{ article.id }}">{{ article.title }}</a></h3>
<p>{{ article.desc }}</p>
<p class="autor"><span class="lm f_l">
{% for tag in article.tag.all %}<a href="/">{{ tag.name }}</a> </span>{% endfor %}<span
class="dtime f_l">{{ article.date_publish | date:'Y-m-d' }}</span><span class="viewnum f_r">浏览(<a
href="/">{{ article.click_count }}</a>)</span><span class="pingl f_r">评论(<a
href="/">{{ article.comment_set.all.count }}</a>)</span></p>
</ul>
</div>
{% endfor %}
</div>
{% include 'pagination.html' %}<!--翻页-->
{% endblock %}

pageination.html

<div id="pagination">
<ul id="pagination-flickr">
{% if article_list.has_previous %}
<li class="previous"><a href="?page={{ article_list.previous_page_number }}{% if request.GET.year %}&year={{ request.GET.year }}{% endif %}{% if request.GET.month %}&month={{ request.GET.month }}{% endif %}{% if request.GET.cid %}&cid={{ request.GET.cid }}{% endif %}">«上一页</a></li>
{% else %}
<li class="previous-off">«上一页</li>
{% endif %}
<li class="active">{{ article_list.number }}/{{ article_list.paginator.num_pages }}</li>
{% if article_list.has_next %}
<li class="next"><a href="?page={{ article_list.next_page_number }}{% if request.GET.year %}&year={{ request.GET.year }}{% endif %}{% if request.GET.month %}&month={{ request.GET.month }}{% endif %}{% if request.GET.cid %}&cid={{ request.GET.cid }}{% endif %}">下一页 »</a></li>
{% else %}
<li class="next-off">下一页 »</li>
{% endif %}
</ul>
</div>

4.运行结果

Django之分页显示文章的更多相关文章

  1. 基于SpringBoot从零构建博客网站 - 分页显示文章列表功能

    显示文章列表一般都是采用分页显示,比如每页10篇文章显示.这样就不用每次就将所有的文章查询出来,而且当文章数量特别多的时候,如果一次性查询出来很容易出现OOM异常. 后台的分页插件采用的是mybati ...

  2. Django 后台 无法显示文章标题

    我们会发现所有的文章都是叫 **object,这样肯定不好,比如我们要修改,如何知道要修改哪个呢? 我们修改一下 blog 中的models.py # coding:utf-8 from django ...

  3. php分页显示文章列表

    <div class="content"> <ul> <?php $querySel = "select * from news where ...

  4. Laravel大型项目系列教程(四)显示文章列表和用户修改文章

    小编心语:不知不觉已经第四部分了,非常感谢很多人给小编提的意见,改了很多bug,希望以后能继续帮小编找找茬~小编也不希望误导大家~这一节,主要讲的 是如何显示文章列表和让用户修改文章,小编预告一下(一 ...

  5. Django中html里的分页显示

    分页一(very low) 因为数据量过大,而又想直观便捷的查看数据,进而通过分页显示就可以完成这项工作 app中views.py LIST=[] #全局定义一个LIST for i in range ...

  6. 利用Flask-SQLAlchemy提供的paginate()方法实现博客文章的分页显示

    在开发blog系统的时候,我们有一个需求,就是要显示作者已经发表的blog文章的列表,或显示作者关注者的文章列表.实现这个功能并不复杂,只需要在存储文章的数据库中过滤出指定作者的文章,然后渲染HTML ...

  7. Django项目:CRM(客户关系管理系统)--19--11PerfectCRM实现King_admin分页显示条数

    登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html list_per_page = 2 #分页条数 list_per_page = 2 #分页条数 ...

  8. day70 cookie & session 前后端交互分页显示

    本文转载自qimi博客,cnblog.liwenzhou.com 概要: 我们的cookie是保存在浏览器中的键值对 为什么要有cookie? 我们在访问浏览器的时候,千万个人访问同一个页面,我们只要 ...

  9. Django自定义分页、bottle、Flask

    一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1"> ...

随机推荐

  1. Objective-C基础笔记(8)Foundation常用类NSString

    一.创建字符串的方法 void stringCreate(){ //方法1 NSString *str1 = @"A String!"; //方法2 NSString *str2 ...

  2. 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...

  3. 2. Spring Boot Controller

    转自:https://blog.csdn.net/catoop/article/details/50501676

  4. Injection of autowired dependencies failed; autowire 自动注入失败,测试类已初始化过了Spring容器。

    1 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creat ...

  5. [D3] Drawing path in D3

    Here we have a force layout with three nodes. In the example, we will link three nodes with line and ...

  6. Codeforces #28 C.Bath Queue (概率dp)

    Codeforces Beta Round #28 (Codeforces format) 题目链接: http://codeforces.com/contest/28/problem/C 题意: 有 ...

  7. stm32关于.o的错误

    是因为没有加入官方库的原因,而且编译出错之后不能跳转到那里.

  8. HDU - 4552 怪盗基德的挑战书 (后缀数组)

    Description "在树最漂亮的那天,当时间老人再次把大钟平均分开时,我会降临在灯火之城的金字塔前.带走那最珍贵的笑容."这是怪盗基德盗取巴黎卢浮宫的<蒙娜丽莎的微笑& ...

  9. UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂)

    UVA10006 - Carmichael Numbers(筛选构造素数表+高速幂) 题目链接 题目大意:假设有一个合数.然后它满足随意大于1小于n的整数a, 满足a^n%n = a;这种合数叫做Ca ...

  10. GitHub 上排名前 100 的 IOS 开源库简介

    主要对当前 GitHub 排名前 100 的项目做一个简单的简介, 方便初学者快速了解到当前 Objective-C 在 GitHub 的情况. 项目名称 项目信息 1. AFNetworking 作 ...