Django之分页显示文章
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之分页显示文章的更多相关文章
- 基于SpringBoot从零构建博客网站 - 分页显示文章列表功能
显示文章列表一般都是采用分页显示,比如每页10篇文章显示.这样就不用每次就将所有的文章查询出来,而且当文章数量特别多的时候,如果一次性查询出来很容易出现OOM异常. 后台的分页插件采用的是mybati ...
- Django 后台 无法显示文章标题
我们会发现所有的文章都是叫 **object,这样肯定不好,比如我们要修改,如何知道要修改哪个呢? 我们修改一下 blog 中的models.py # coding:utf-8 from django ...
- php分页显示文章列表
<div class="content"> <ul> <?php $querySel = "select * from news where ...
- Laravel大型项目系列教程(四)显示文章列表和用户修改文章
小编心语:不知不觉已经第四部分了,非常感谢很多人给小编提的意见,改了很多bug,希望以后能继续帮小编找找茬~小编也不希望误导大家~这一节,主要讲的 是如何显示文章列表和让用户修改文章,小编预告一下(一 ...
- Django中html里的分页显示
分页一(very low) 因为数据量过大,而又想直观便捷的查看数据,进而通过分页显示就可以完成这项工作 app中views.py LIST=[] #全局定义一个LIST for i in range ...
- 利用Flask-SQLAlchemy提供的paginate()方法实现博客文章的分页显示
在开发blog系统的时候,我们有一个需求,就是要显示作者已经发表的blog文章的列表,或显示作者关注者的文章列表.实现这个功能并不复杂,只需要在存储文章的数据库中过滤出指定作者的文章,然后渲染HTML ...
- Django项目:CRM(客户关系管理系统)--19--11PerfectCRM实现King_admin分页显示条数
登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html list_per_page = 2 #分页条数 list_per_page = 2 #分页条数 ...
- day70 cookie & session 前后端交互分页显示
本文转载自qimi博客,cnblog.liwenzhou.com 概要: 我们的cookie是保存在浏览器中的键值对 为什么要有cookie? 我们在访问浏览器的时候,千万个人访问同一个页面,我们只要 ...
- Django自定义分页、bottle、Flask
一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1"> ...
随机推荐
- setInterval 传参数
<script type="text/javascript" > window.onload=function(){ for(var i=1;i<3;i++){ ...
- 3/21 Django框架 模板路径及模板过滤器 1.模板路径查找
3/21 Django框架 模板路径及模板过滤器 1.模板路径查找 先找settings.py里的TEMPLATES列表下的DIRS路径.如果APP_DIRS为True,还会到注册了的APP文件夹下依 ...
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- [React] Render Text Only Components in React 16
In this session we create a comment component to explore how to create components that only render t ...
- [Node & Tests] Intergration tests for Authentication
For intergration tests, always remember when you create a 'mass' you should aslo clean up the 'mass' ...
- Android提示版本号更新操作流程
Android提示版本号更新操作流程 2014年5月8日: andorid的app应用中都会有版本号更新的操作,今天空暇的时候就花了点心思弄了一下.主要技术方面用到了AsyncTask异步载入.htt ...
- HTML基础第八讲---序列卷标
转自:https://i.cnblogs.com/posts?categoryid=1121494 什么叫做序列卷标?其实,这是一个大家都蛮熟悉的东西,只是在网页中换个名称来称呼罢了,序列卷标的功能在 ...
- 06 Jenkins自动化构建
• 在之前的freestyle任务基础上,实现构建功能• 检查maven环境• 构建命令:mvn clean compile• 归档构建产物• 演练参数的使用• 通过配置Trigger进行自动构建• ...
- 洛谷—— P1967 货车运输 || COGS——C 1439. [NOIP2013]货车运输
https://www.luogu.org/problem/show?pid=1967#sub || http://www.cogs.pro/cogs/problem/problem.php?pi ...
- Project Euler 516 5-smooth totients (数论)
题目链接: https://projecteuler.net/problem=516 题目: \(5\)-smooth numbers are numbers whose largest prime ...