7 Django分页器文章分页
1.复习





2。这节课要解决的问题?


3。分页的原理




4.准备工作

(1)创建Django项目
C:\Users\Administrator\Desktop\root3>django-admin startproject tenmins
(2)修改python3
#!/usr/bin/env python3
(3).创建app
C:\Users\Administrator\Desktop\root3\tenmins>python manage.py startapp website

(4)添加静态文件,
C:\Users\Administrator\Desktop\root3\tenmins\website>mkdir templates C:\Users\Administrator\Desktop\root3\tenmins\website>mkdir static
'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\','/')],

Model层:
(1)Video表字段
from django.db import models class Video(models.Model):
"""Video表字段"""
title = models.CharField(null=True, blank=True, max_length=300) # 文章标题
content = models.TextField(null=True) # 视频解说内容
url_image = models.URLField(null=True, blank=True) # 封面 网上的图片
editors_choice = models.BooleanField(default=False) # 文章分类用的 def __str__(self):
return self.title
(2)向后台注册,更新数据库,创建admin




View层
(1)view视图
from django.shortcuts import render
from website.models import Video
# Create your views here. def listing(request):
context = {}
video_list = Video.objects.all()
context['video_list'] = video_list
listing_page = render(request, 'listing.html', context)
return listing_page
(2) url地址
from django.conf.urls import url
from django.contrib import admin
from website.views import listing urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^list/$', listing, name='list'),
]
Template层:
(3)模板语言替换video.title
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="../static/css/semantic.css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="../static/css/list_custom.css" media="screen" title="no title" charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Oswald|Raleway" rel="stylesheet"> </head> <body>
<div class="ui inverted top fixed menu borderless red menu">
<div class="header item">
<div class="ui image">
<img src="../static/images/tenlogo.png" alt="">
</div>
</div> <div class="right menu">
<div class="item">
<h5 class="ui inverted header">
<div class="ui mini circular image">
<img src="../static/images/hou30.jpg" alt="">
</div> <span>admin</span> </h5>
</div>
<div class="item"> <a href="#logout/" class="ui inverted circular button">Logout</a> </div>
</div>
</div>
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="item">
All
</a>
<a class="item">
New
</a> <a class="active item" href="#list/editors">
Editor's
</a> </div>
</div> <div class="ui basic segment container content"> <div class="ui three column grid"> {% for video in video_list %}
<div class="column">
<a class="ui fluid card" href="#detail/215">
<div class="image">
<img src="../static/images/img1.jpg" alt="" style="height:200px;object-fit: cover;">
</div>
</a> <div class="title header" href="/detail/215">{{ video.title }}</div> <i class="icon grey unhide"></i>
<span style="color:#bbbbbb">10K</span>
<span class="" style="color:rgb(226, 226, 226)">|</span>
<i class="icon grey checkmark"></i>
<span style="color:#bbbbbb"> 10 people got it</span> </div>
{% endfor %} </div>
</div> <div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu"> <a href="#" class="item">
<i class="icon red left arrow"></i>
</a> <a href="#" class="item">
<i class="icon red right arrow"></i>
</a>
</div>
</div>
</body> </html>

5.faker库的使用
参考博客
http://hao.jobbole.com/python-faker/
http://blog.csdn.net/xiaodongxiexie/article/details/77067492
https://github.com/joke2k/faker
(1)安装faker库
> pip install faker
(2)导入faker库
#第一种
from faker import Faker #Faker.py 文件
from faker.generator import Generator
from faker.factory import Factory Faker = Factory.create #第二种
from faker import Factory
(3)生成虚假数据
>>> faker_1 = faker.Faker()
>>> faker_1.name()
'Thomas Lynn'
>>> faker_1.text()
'Boy civil better tax policy realize. And least dinner seem both rate. Never simply case test opportunity.'
>>> faker_1.text() 'Environmental offer size west.\nReality billion necessary all candidate third. Across go different human.\nPiece production section table painting magazine
drop. Friend explain wife couple.'
>>>
>>> faker_1.pybool()
False
>>> faker_1.pybool()
False

图片文件url.txt
https://pixabay.com/static/uploads/photo/2016/08/25/19/10/mill-1620440__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/10/17/47/eggplant-1659784__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/16/17/20/elevator-1598431__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/13/58/algodones-dunes-1654439__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/20/52/milky-way-1655504__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/10/19/56/lions-1660044__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/10/23/48/bridge-1660417__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/18/38/dovetail-1655098__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/17/18/15/tabletop-photography-1601184__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/04/20/14/sunset-1645103__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/19/18/50/fruit-1605921__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/15/20/29/olive-oil-1596417__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/13/35/doll-1636128__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/12/00/starry-sky-1654074__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/19/43/sunrise-1637376__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/19/53/pocket-watch-1637396__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/25/11/01/landscape-1619283__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/29/21/29/lotus-flower-1629225__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/07/16/19/bremen-town-musicians-1651945__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/25/19/10/mill-1620440__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/27/22/55/sunset-1625073__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/30/11/59/cobweb-1630493__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/18/24/rk5161-1636868__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/27/20/03/berries-1546125__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/20/44/elan-1623088__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/09/21/54/yellowstone-national-park-1581879__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/15/15/55/dachshund-1519374__340.jpg
https://pixabay.com/static/uploads/photo/2016/05/28/07/04/women-1421096__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/28/17/54/sunset-1626515__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/13/15/28/lake-tahoe-1590923__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/11/09/40/water-lily-1585178__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/31/19/26/fischer-1559753__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/11/22/35/tomatoes-1587130__340.jpg
https://pixabay.com/static/uploads/photo/2016/04/22/16/42/mt-fuji-1346096__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/12/23/47/yosemite-1590013__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/01/20/13/girl-1561989__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/17/14/geese-1622692__340.jpg
https://pixabay.com/static/uploads/photo/2016/03/09/03/49/tarantula-nebula-1245253__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/11/23/55/redwood-national-park-1587301__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/17/33/landscape-1622739__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/21/40/eurasian-eagle-owl-1642795__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/28/22/59/dahlia-1627138__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/14/19/motion-1641793__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/04/08/46/butterfly-1643510__340.jpg
https://pixabay.com/static/uploads/photo/2015/12/17/02/22/milky-way-1096815__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/04/12/05/sun-flower-1643794__340.jpg
https://pixabay.com/static/uploads/photo/2015/08/20/02/46/arch-896900__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/23/17/flowers-1642964__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/18/44/dahlia-1642464__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/14/15/motion-1641781__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/14/15/doll-1636212__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/15/13/motion-1641979__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/19/35/griffith-observatory-1642514__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/12/23/58/fig-1590016__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/09/21/54/patagonia-1581878__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/31/11/30/san-francisco-1633202__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/18/40/dahlia-1642455__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/18/35/mackinac-bridge-1642445__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/20/44/sunrise-1642695__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/14/15/sunrise-1641780__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/27/17/34/camel-1624643__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/28/23/18/sun-flower-1627179__340.jpg
https://pixabay.com/static/uploads/photo/2016/02/13/16/15/sunset-1198134__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/28/18/48/jefferson-memorial-1626580__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/27/23/07/salt-lake-1625125__340.jpg
https://pixabay.com/static/uploads/photo/2016/06/05/21/46/goat-1438254__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/16/13/50/sun-flower-1521852__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/17/30/mountains-1622731__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/25/14/25/rose-1619566__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/15/08/22/greece-1594689__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/31/13/31/chile-house-1633433__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/31/14/35/italy-1633682__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/31/14/23/father-1633655__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/31/11/29/detail-1633198__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/20/44/elan-1623087__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/12/20/14/yellowstone-1589614__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/20/27/amber-1623059__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/26/21/02/arielle-1543832__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/11/23/48/pismo-beach-1587289__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/12/14/12/catalina-island-1588654__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/10/17/47/eggplant-1659784__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/16/17/20/elevator-1598431__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/13/58/algodones-dunes-1654439__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/20/52/milky-way-1655504__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/10/19/56/lions-1660044__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/10/23/48/bridge-1660417__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/18/38/dovetail-1655098__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/17/18/15/tabletop-photography-1601184__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/04/20/14/sunset-1645103__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/19/18/50/fruit-1605921__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/15/20/29/olive-oil-1596417__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/13/35/doll-1636128__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/08/12/00/starry-sky-1654074__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/19/43/sunrise-1637376__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/19/53/pocket-watch-1637396__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/25/11/01/landscape-1619283__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/29/21/29/lotus-flower-1629225__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/07/16/19/bremen-town-musicians-1651945__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/25/19/10/mill-1620440__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/27/22/55/sunset-1625073__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/30/11/59/cobweb-1630493__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/18/24/rk5161-1636868__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/27/20/03/berries-1546125__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/20/44/elan-1623088__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/09/21/54/yellowstone-national-park-1581879__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/15/15/55/dachshund-1519374__340.jpg
https://pixabay.com/static/uploads/photo/2016/05/28/07/04/women-1421096__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/28/17/54/sunset-1626515__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/13/15/28/lake-tahoe-1590923__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/11/09/40/water-lily-1585178__340.jpg
https://pixabay.com/static/uploads/photo/2016/07/31/19/26/fischer-1559753__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/11/22/35/tomatoes-1587130__340.jpg
https://pixabay.com/static/uploads/photo/2016/04/22/16/42/mt-fuji-1346096__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/12/23/47/yosemite-1590013__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/01/20/13/girl-1561989__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/17/14/geese-1622692__340.jpg
https://pixabay.com/static/uploads/photo/2016/03/09/03/49/tarantula-nebula-1245253__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/11/23/55/redwood-national-park-1587301__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/17/33/landscape-1622739__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/21/40/eurasian-eagle-owl-1642795__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/28/22/59/dahlia-1627138__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/14/19/motion-1641793__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/04/08/46/butterfly-1643510__340.jpg
https://pixabay.com/static/uploads/photo/2015/12/17/02/22/milky-way-1096815__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/04/12/05/sun-flower-1643794__340.jpg
https://pixabay.com/static/uploads/photo/2015/08/20/02/46/arch-896900__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/23/17/flowers-1642964__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/18/44/dahlia-1642464__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/14/15/motion-1641781__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/01/14/15/doll-1636212__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/15/13/motion-1641979__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/19/35/griffith-observatory-1642514__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/12/23/58/fig-1590016__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/09/21/54/patagonia-1581878__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/31/11/30/san-francisco-1633202__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/18/40/dahlia-1642455__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/18/35/mackinac-bridge-1642445__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/20/44/sunrise-1642695__340.jpg
https://pixabay.com/static/uploads/photo/2016/09/03/14/15/sunrise-1641780__340.jpg
https://pixabay.com/static/uploads/photo/2016/02/13/16/15/sunset-1198134__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/28/18/48/jefferson-memorial-1626580__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/26/17/30/mountains-1622731__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/25/14/25/rose-1619566__340.jpg
https://pixabay.com/static/uploads/photo/2016/08/31/13/31/chile-house-1633433__340.jpg
https://cdn.pixabay.com/photo/2016/11/21/12/26/concrete-1845046_1280.jpg
(4)生成数据库表字段,然后注释掉

(5)model.py文件
from django.db import models
from faker import Factory class Video(models.Model):
"""Video表字段"""
title = models.CharField(null=True, blank=True, max_length=300) # 文章标题
content = models.TextField(null=True) # 视频解说内容
url_image = models.URLField(null=True, blank=True) # 封面 网上的图片
editors_choice = models.BooleanField(default=False) # 文章分类用的 def __str__(self):
return self.title # f = open('/Users/Administrator/Desktop/111.txt', 'r')
# fake = Factory.create()
#
# for url in f.readlines():
# v = Video(
# title=fake.text(max_nb_chars=90),
# content=fake.text(max_nb_chars=3000),
# url_image=url,
# editors_choice=fake.pybool(),
# )
# v.save()
(6) 修改Tempate模板的图片


(7)listing.html
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="../static/css/semantic.css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="../static/css/list_custom.css" media="screen" title="no title" charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Oswald|Raleway" rel="stylesheet"> </head> <body>
<div class="ui inverted top fixed menu borderless red menu">
<div class="header item">
<div class="ui image">
<img src="../static/images/tenlogo.png" alt="">
</div>
</div> <div class="right menu">
<div class="item">
<h5 class="ui inverted header">
<div class="ui mini circular image">
<img src="../static/images/hou30.jpg" alt="">
</div> <span>admin</span> </h5>
</div>
<div class="item"> <a href="#logout/" class="ui inverted circular button">Logout</a> </div>
</div>
</div>
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="item">
All
</a>
<a class="item">
New
</a> <a class="active item" href="#list/editors">
Editor's
</a> </div>
</div> <div class="ui basic segment container content"> <div class="ui three column grid"> {% for video in video_list %}
<div class="column">
<a class="ui fluid card" href="#detail/215">
<div class="image">
<img src="{{ video.url_image }}" alt="" style="height:200px;object-fit: cover;">
</div>
</a> <div class="title header" href="/detail/215">{{ video.title }}</div> <i class="icon grey unhide"></i>
<span style="color:#bbbbbb">10K</span>
<span class="" style="color:rgb(226, 226, 226)">|</span>
<i class="icon grey checkmark"></i>
<span style="color:#bbbbbb"> 10 people got it</span> </div>
{% endfor %} </div>
</div> <div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu"> <a href="#" class="item">
<i class="icon red left arrow"></i>
</a> <a href="#" class="item">
<i class="icon red right arrow"></i>
</a>
</div>
</div>
</body> </html>
6.实现分页功能:Django自带的分页器


from django.shortcuts import render
from website.models import Video
from django.core.paginator import Paginator
# Create your views here. def listing(request):
context = {}
video_list = Video.objects.all() page_rebot = Paginator(video_list, 9) # 每页9个数据
video_list = page_rebot.page(request.GET.get('page')) # get方法取哪一页 context['video_list'] = video_list
listing_page = render(request, 'listing.html', context)
return listing_page




7.实现异常处理

(1)?page=9999或者?page=3kdjaf 两种错误
- 999 重定向到实际的最后一页;或者404错误
- ?page=34jfad 页码不是整数数字

(2) 异常处理



(3)实验结果测试




(3)代码
from django.shortcuts import render, Http404
from website.models import Video
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# Create your views here. def listing(request):
context = {}
video_list = Video.objects.all() page_rebot = Paginator(video_list, 9) # 每页9个数据
page_num = request.GET.get('page') try:
video_list = page_rebot.page(page_num) # get方法取哪一页
except EmptyPage:
video_list = page_rebot.page(page_rebot.num_pages) # 999加载最后一页
#raise Http404('EmptyPage') #返回404错误
except PageNotAnInteger:
video_list = page_rebot.page(1) # 432jds 加载第一页 context['video_list'] = video_list
listing_page = render(request, 'listing.html', context)
return listing_page
8.在模板层实现分页功能





(1)被分页器装载的列表会有:两个方法
- video_list.has_previous和 video_list.has_next
- 会返回True或False






(2)代码
{% if video_list.has_previous %}
<a href="?page={{ video_list.previous_page_number }}" class="item">
<i class="icon red left arrow"></i>
</a>
{% else %}
<a href="?page={{ video_list.start_index }}" class="disabled item">
<i class="icon left arrow"></i>
</a>
{% endif %}
{% if video_list.has_next %}
<a href="?page={{ video_list.next_page_number }" class="item">
<i class="icon red right arrow"></i>
</a>
{% else %}
<a href="?page={{ video_list.end_index }" class="disabled item">
<i class="icon right arrow"></i>
</a>
{% endif %}
9.实现分类功能

(1)实现分类功能下的分页


(2)允许一个view可以匹配多个网址url

(3)可选参数

from django.shortcuts import render, Http404
from website.models import Video
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# Create your views here. def listing(request, cate=None): #cate可选默认参数
context = {} if cate is None:
video_list = Video.objects.all()
if cate == 'editors':
video_list = Video.objects.filter(editors_choice=True)
else:
video_list = Video.objects.all() page_rebot = Paginator(video_list, 9) # 每页9个数据
page_num = request.GET.get('page') try:
video_list = page_rebot.page(page_num) # get方法取哪一页
except EmptyPage:
video_list = page_rebot.page(page_rebot.num_pages) # 999加载最后一页
#raise Http404('EmptyPage') #返回404错误
except PageNotAnInteger:
video_list = page_rebot.page(1) # 432jds 加载第一页 context['video_list'] = video_list
listing_page = render(request, 'listing.html', context)
return listing_page
(4)a标签的href
<a class="active item" href="{% url 'list' %}editors">
Editor's
</a>

(5)判断editor是否被激活
- url是否有editors字段





(6)listing.html 代码
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="../static/css/semantic.css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="../static/css/list_custom.css" media="screen" title="no title" charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Oswald|Raleway" rel="stylesheet"> </head> <body>
<div class="ui inverted top fixed menu borderless red menu">
<div class="header item">
<div class="ui image">
<img src="../static/images/tenlogo.png" alt="">
</div>
</div> <div class="right menu">
<div class="item">
<h5 class="ui inverted header">
<div class="ui mini circular image">
<img src="../static/images/hou30.jpg" alt="">
</div> <span>admin</span> </h5>
</div>
<div class="item"> <a href="#logout/" class="ui inverted circular button">Logout</a> </div>
</div>
</div>
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="item">
All
</a>
<a class="item">
New
</a> {% if 'editors' in request.path %}
<a class="active item" href="{% url 'list' %}editors">
Editor's
</a>
{% else %}
<a class="item" href="{% url 'list' %}editors">
Editor's
</a>
{% endif %} </div>
</div> <div class="ui basic segment container content"> <div class="ui three column grid"> {% for video in video_list %}
<div class="column">
<a class="ui fluid card" href="#detail/215">
<div class="image">
<img src="{{ video.url_image }}" alt="" style="height:200px;object-fit: cover;">
</div>
</a> <div class="title header" href="/detail/215">{{ video.title }}</div> <i class="icon grey unhide"></i>
<span style="color:#bbbbbb">10K</span>
<span class="" style="color:rgb(226, 226, 226)">|</span>
<i class="icon grey checkmark"></i>
<span style="color:#bbbbbb"> 10 people got it</span> </div>
{% endfor %} </div>
</div> <div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu"> {% if video_list.has_previous %}
<a href="?page={{ video_list.previous_page_number }}" class="item">
<i class="icon red left arrow"></i>
</a>
{% else %}
<a href="?page={{ video_list.start_index }}" class="disabled item">
<i class="icon left arrow"></i>
</a>
{% endif %} {% if video_list.has_next %}
<a href="?page={{ video_list.next_page_number }" class="item">
<i class="icon red right arrow"></i>
</a>
{% else %}
<a href="?page={{ video_list.end_index }" class="disabled item">
<i class="icon right arrow"></i>
</a>
{% endif %} </div>
</div>
</body> </html>
10.上传本地图片

(1)添加上传图片字段

from django.db import models
from faker import Factory class Video(models.Model):
"""Video表字段"""
title = models.CharField(null=True, blank=True, max_length=300) # 文章标题
content = models.TextField(null=True) # 视频解说内容
url_image = models.URLField(null=True, blank=True) # 封面 网上的图片 cover = models.FileField(upload_to='cover_image', null=True) #上传图片字段 editors_choice = models.BooleanField(default=False) # 文章分类用的 def __str__(self):
return self.title
(2)setting.py 添加上传图片路径

MEDIA_URL = '/upload/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload').replace("//", "/")
(3)setting.DEBUG开发模式开启的时候:在本地查找
关闭这个模式,服务器会帮我们解决

from django.conf.urls import url
from django.contrib import admin
from website.views import listing from django.conf import settings
from django.conf.urls.static import static urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^list/$', listing, name='list'),
url(r'^list/(?P<cate>[A-Za-z]+)$', listing, name='list'), #cate 变量
] if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
(4) 合并数据库

(5)后台上传文件


(6)Template 层:如果本地上传的文件存在就不用网络的


7 Django分页器文章分页的更多相关文章
- Django - Cookie、Session、自定义分页和Django分页器
2. 今日内容 https://www.cnblogs.com/liwenzhou/p/8343243.html 1. Cookie和Session 1. Cookie 服务端: 1. 生成字符串 2 ...
- Django分页器的设置
Django分页器的设置 有时候在页面中数据有多条时很显然需要进行分页显示,那么在python中django可以这样设置一个分页处理 怎么样去设置呢? 我们要用到 Django 中的 Pagin ...
- Django 分页器的使用
Django 分页器的使用 Django作为Python Web开发框架的一哥,提供了企业级网站开发所需要的几乎所有功能,其中就包括自带分页功能.利用Django自带的Paginator类,我们可以很 ...
- Django分页器和自定义分页器
一.自定义分页器 import copy class Pagination(): def __init__(self,request,current_page,all_data_num,each_pa ...
- django -----分页器组件
分页器组件 本文目录 1 Django的分页器(paginator)简介 2 应用View层 3 模版层 index.html 4 扩展 回到目录 1 Django的分页器(paginator)简介 ...
- Django 2.0 学习(19):Django 分页器
Django 分页器 要使用Django实现分页功能,必须从Django中导入Paginator模块(painator - 分页器) views.py from django.shortcuts im ...
- Django 分页器 缓存 信号 序列化
阅读目录 分页器 缓存 信号 序列化 Django分页器 (paginator) 导入 from django.core.paginator import Paginator, EmptyPage, ...
- django: django rest framework 分页
django: django rest framework 分页 2018年06月22日 13:41:43 linux_player_c 阅读数:665更多 所属专栏: django 实战 版权声 ...
- django视图之分页
在网站开发时,肯定会遇到分页的事情需要处理,在django中也是如此,在Django中处理分页一般会使用到两个类django.core.paginator.Paginator和django.core. ...
随机推荐
- SharePoint 和 Windows Phone 7 开发人员培训资源
该工具包包括单位数目: Windows Phone 7 发展简介 在本单元中,您将了解真实的消费信息存储在 SharePoint 中与你用 Visual Studio 写 Windows Phone ...
- HttpWebRequest Post请求webapi
1.WebApi设置成Post请求在方法名加特性[HttpPost]或者方法名以Post开头如下截图: 2.使用(服务端要与客户端对应起来)[单一字符串方式]:注意:ContentType = &qu ...
- 使用md5的密码加密,处理用户的密码
需求 1.新增用户保存:使用md5的密码加密,如果用户没有填写密码,设置初始密码“123”: 2.修改用户保存:使用md5的加密加密 *如果修改了密码,需要进行md5的密码加密: *如果没有修改密码, ...
- 08、Spark常用RDD变换
08.Spark常用RDD变换 8.1 概述 Spark RDD内部提供了很多变换操作,可以使用对数据的各种处理.同时,针对KV类型的操作,对应的方法封装在PairRDDFunctions trait ...
- POJ - 3109 Inner Vertices
不存在-1的情况,而且最多一轮就结束了.如果新增加的黑点v0会产生新的黑点v1,那么v0和v1肯定是在一条轴上的,而原来这条轴上已经有黑点了. 离散以后扫描线统计,往线段上插点,然后查询区间上点数. ...
- 既然红黑树那么好,为啥hashmap不直接采用红黑树,而是当大于8个的时候才转换红黑树?
因为红黑树需要进行左旋,右旋操作, 而单链表不需要,以下都是单链表与红黑树结构对比.如果元素小于8个,查询成本高,新增成本低如果元素大于8个,查询成本低,新增成本高 https://bbs.csdn. ...
- python 爬poj.org的题目
主要是正则表达式不熟练,基础知识不扎实,函数也不怎么会用,下次再深入了解这3个函数吧. 主要是一个翻页的功能,其实,就是通过一个url替换一下数字,然后得到一个新的url,再找这个新的链接的信息. # ...
- 【转】startActivityForResult和setResult详解
startActivityForResult与startActivity的不同之处在于:1.startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startAct ...
- 2018.7.8 xmlhttp.readyState==4 && xmlhttp.status==200是什么意思
在做DOM模型的XML实验的时候遇到了问题 代码实例: xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && ...
- 复杂链表的复制 -python编写
题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...