运维开发笔记整理-数据分页

            运维开发笔记整理-数据分页

                                       作者:尹正杰 

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.创建测试数据(批量创建1000个用户)

C:\Users\yinzhengjie\softwares\GitHub\DevOps>python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
登录ipython(C:\Users\yinzhengjie\softwares\GitHub\DevOps>python manage.py shell)
C:\Users\yinzhengjie\softwares\GitHub\DevOps>python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from django.contrib.auth.models import User
>>>
>>> username = "yinzhengjie"
>>>
>>> for i in range(1000):
...  name = "{}_{}".format(username,i)
...  User.objects.create_user(name,"{}@yinzhengjie.org.cn".format(name),"123")
...
























........
>>> User.objects.all()
, , , , , , , , , , , , , <User: yinzhengjie
_9>, , , , , , , '...(remaining elements truncated)...']>
>>>
查询用户表的所有数据(>>> User.objects.all())
>>> User.objects.all()[:10]
, , , , , , , , , ]>
>>>
查询前10条数据(>>> User.objects.all()[:10])
>>> queryset = User.objects.all()[:10]
>>>
>>> queryset.values()
'id': 1, 'password': 'pbkdf2_sha256$36000$mpkI6GLbFQxO$92QgD28jd838LlgzTsnFsvw+2CbTAgK+LgzS3G40h/k=', 'last_login': datetime.datetime(2019, 1, 14, 8, 7, 20, tzinfo=), 'is_superuser': False, 'username': 'yinzhengjie', 'first_name': '', 'last_name': '', 'email': 'yinzhengjie@yinzhengjie.o
rg.cn', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 9, 13, 3, 26, tzinfo=)}, {'id': 2, 'password': 'pbkdf2_sha256$36000$AJNnOcpMRfAB$wvsTcO221K9c/ogSIW8WuK4ti4iRbVp6LliFJnq0PlA=', 'last_login': None, 'is_superuser': True, 'username': 'admin', 'first_name': '', 'l
ast_name': '', 'email': 'admin@yinzhengjie.org.cn', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 9, 13, 7, 43, tzinfo=)}, {'id': 3, 'password': 'pbkdf2_sha256$36000$9xEXixjbrwgW$VRE2gdFRjnI6E3mc4nor3rUqBP97wai8xqulzgRnwPQ=', 'last_login': datetime.datetime(2019, 1,11, 9, 54, 22, tzinfo=), 'is_superuser': False, 'username': 'chenfei', 'first_name': '', 'last_name': '', 'email': 'chenfei@yinzhengjie.org.cn', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 11, 9, 24, 6, tzinfo=)}, {'id': 4, 'password': 'pbkdf2_sha256$36000$
Frpw9hmKTaKa$B4dVdqiM4K+2yFLd3DfQV74mv2tllwnZxjJPFjhEh2A=', 'last_login': datetime.datetime(2019, 1, 11, 9, 35, 32, tzinfo=), 'is_superuser': True, 'username': 'jenny', 'first_name': '', 'last_name': '', 'email': '', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 11,9, 35, 17, tzinfo=)}, {'id': 5, 'password': 'pbkdf2_sha256$36000$Igzct2aN2kIn$KJUA3qbjZYT8Mz8UubgptT425CrAZI3lr1L47tOYnKA=', 'last_login': None, 'is_superuser': False, 'username': 'yinzhengjie_0', 'first_name': '', 'last_name': '', 'email': 'yinzhengjie_0@yinzhengjie.org.cn', 'is_staff': False, 'i
s_active': True, 'date_joined': datetime.datetime(2019, 1, 15, 6, 39, 47, tzinfo=)}, {'id': 6, 'password': 'pbkdf2_sha256$36000$pVgOfvV7xnLA$v/HdnmOrON5yXQm6Tu/ESQLExwzsDLS1HYmB420STw0=', 'last_login': None, 'is_superuser': False, 'username': 'yinzhengjie_1', 'first_name': '', 'last_name': '', 'ema
il': 'yinzhengjie_1@yinzhengjie.org.cn', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 15, 6, 39, 47, tzinfo=)}, {'id': 7, 'password': 'pbkdf2_sha256$36000$AcBse0DrqytQ$UDTl3i8wozYnEcRCanRcTxF06sQCdW4ezDNDDOBW6Iw=', 'last_login': None, 'is_superuser': False, 'usern
ame': 'yinzhengjie_2', 'first_name': '', 'last_name': '', 'email': 'yinzhengjie_2@yinzhengjie.org.cn', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 15, 6, 39, 47, tzinfo=)}, {'id': 8, 'password': 'pbkdf2_sha256$36000$hkSzLweU5YB1$TNwHHuQ3X+4s2bw31yLc1oxYfo+2OhE0CR
FQrZi6QtA=', 'last_login': None, 'is_superuser': False, 'username': 'yinzhengjie_3', 'first_name': '', 'last_name': '', 'email': 'yinzhengjie_3@yinzhengjie.org.cn', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 15, 6, 39, 47, tzinfo=)}, {'id': 9, 'password': 'pbkdf
2_sha256$36000$9MCEySYs7ioI$XlA3AAUeOYY+IbGEREQTLMEHyulzcenEDRgKTzUlUns=', 'last_login': None, 'is_superuser': False, 'username': 'yinzhengjie_4', 'first_name': '', 'last_name': '', 'email': 'yinzhengjie_4@yinzhengjie.org.cn', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019,
1, 15, 6, 39, 47, tzinfo=)}, {'id': 10, 'password': 'pbkdf2_sha256$36000$LWtenjjciVrx$OY49hPmpRJwqrxtyb16MY8vbWz9tZtgqUiF72obodyQ=', 'last_login': None, 'is_superuser': False, 'username': 'yinzhengjie_5', 'first_name': '', 'last_name': '', 'email': 'yinzhengjie_5@yinzhengjie.org.cn', 'is_staff': Fa
lse, 'is_active': True, 'date_joined': datetime.datetime(2019, 1, 15, 6, 39, 47, tzinfo=)}]>
>>>
查看钱10条数据的详细信息(User.objects.all()[:10].values()) 
C:\Users\yinzhengjie\softwares\GitHub\DevOps>python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from django.contrib.auth.models import User
>>>
>>> queryset = User.objects.all()[:10]
>>>
>>> queryset.values("username","email")
'username': 'yinzhengjie', 'email': 'yinzhengjie@yinzhengjie.org.cn'}, {'username': 'admin', 'email': 'admin@yinzhengjie.org.cn'}, {'username': 'chenfei', 'email': 'chenfei@yinzhengjie.org.cn'}, {'username': 'jenny', 'email': ''}, {'username': 'yinzhengjie_0', 'email': 'yinzhengjie_0@yinzhen
gjie.org.cn'}, {'username': 'yinzhengjie_1', 'email': 'yinzhengjie_1@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_2', 'email': 'yinzhengjie_2@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_3', 'email': 'yinzhengjie_3@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_4', 'email': 'yinzhengjie_4@yinzhen
gjie.org.cn'}, {'username': 'yinzhengjie_5', 'email': 'yinzhengjie_5@yinzhengjie.org.cn'}]>
>>>
只查询用户名和密码( queryset.values("username","email"))
C:\Users\yinzhengjie\softwares\GitHub\DevOps>python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from django.contrib.auth.models import User
>>>
>>> queryset = User.objects.all()[:10]
>>>
>>> queryset.values("username","email")
'username': 'yinzhengjie', 'email': 'yinzhengjie@yinzhengjie.org.cn'}, {'username': 'admin', 'email': 'admin@yinzhengjie.org.cn'}, {'username': 'chenfei', 'email': 'chenfei@yinzhengjie.org.cn'}, {'username': 'jenny', 'email': ''}, {'username': 'yinzhengjie_0', 'email': 'yinzhengjie_0@yinzhen
gjie.org.cn'}, {'username': 'yinzhengjie_1', 'email': 'yinzhengjie_1@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_2', 'email': 'yinzhengjie_2@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_3', 'email': 'yinzhengjie_3@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_4', 'email': 'yinzhengjie_4@yinzhen
gjie.org.cn'}, {'username': 'yinzhengjie_5', 'email': 'yinzhengjie_5@yinzhengjie.org.cn'}]>
>>>
>>> list(queryset.values("username","email"))
[{'username': 'yinzhengjie', 'email': 'yinzhengjie@yinzhengjie.org.cn'}, {'username': 'admin', 'email': 'admin@yinzhengjie.org.cn'}, {'username': 'chenfei', 'email': 'chenfei@yinzhengjie.org.cn'}, {'username': 'jenny', 'email': ''}, {'username': 'yinzhengjie_0', 'email': 'yinzhengjie_0@yinzhengjie.org.c
n'}, {'username': 'yinzhengjie_1', 'email': 'yinzhengjie_1@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_2', 'email': 'yinzhengjie_2@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_3', 'email': 'yinzhengjie_3@yinzhengjie.org.cn'}, {'username': 'yinzhengjie_4', 'email': 'yinzhengjie_4@yinzhengjie.org.c
n'}, {'username': 'yinzhengjie_5', 'email': 'yinzhengjie_5@yinzhengjie.org.cn'}]
>>>
将查询的结果转换成JSON格式(>>> list(queryset.values("username","email")))

 

二.文档浏览器(mac和windows文档)

1>.window登录官网(https://devdocs.io)

2>.mac文档(https://kapeli.com/dash) 

  该文档需要授权,因为它是收费的。 

  关于paginator的文档,详情请参考:https://devdocs.io/django~1.11/topics/pagination#django.core.paginator.Paginator 。

  关于page的文档,详情请参考:https://devdocs.io/django~1.11/topics/pagination#django.core.paginator.Page。

  

三.数据分页原理案例

   dashboard的url.py配置文件,如下图所示:

#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/from django.conf.urls import url
from . import  viewsurlpatterns = [url(r"^login/",views.LoginView.as_view()),url(r"^index/", views.MyPageView.as_view()),
]

  编写对应的views.py配置文件:

#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/from django.http import HttpResponse,JsonResponse
from django.shortcuts import  render
from django.views import View
from django.contrib.auth.models import Userclass LoginView(View):def get(self,request):return render(request,"login.html")def post(self,request):print("调用了POST方法!")return HttpResponse("post...")class MyPageView(View):def get(self,request,*args,**kwargs):#定义每个页码显示信息的条数Number_of_per_page = 10try:#获取到用户传递过来的查询页码page = int(request.GET.get("page",1))except:#如果用户没有传递要查询的页码,我们这里给其设置一个默认值1,即显示第一页。page = 1#定义查询的起始位置end = page * 10start = end - 10#定义查询的语句,注意这里并没有去数据库直接查询语句,这里只是定义好了要查询的SQL语句queryset =  User.objects.all()[start:end]#触发SQL语句,并将查询的结果转换成JSON格式data = list(queryset.values("id","username","email"))return JsonResponse(data,safe=False)

  通过浏览器访问,如下图所示:

 

 

四.Paginator和Page

1>.Paginator

C:\Users\yinzhengjie\softwares\GitHub\DevOps>python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from django.core.paginator import Paginator
>>>>>> from django.contrib.auth.models import User
>>>
>>> queryset = User.objects.all()
>>>
>>> paginator = Paginator(queryset,20)          #实例化Paginator对象,下面的提示可以不用处理,说是咱们没有对数据进行排序
:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'django.contrib.auth.models.User'> QuerySet.
>>>
>>> paginator.count
1004
>>>
Paginator的count属性,查询所有页面对objects总数(>>> paginator.count)
>>> paginator.num_pages
51
>>>
Paginator的num_pages属性,查询页面总数(>>> paginator.num_pages)
>>> paginator.page_range
range(1, 52)
>>>
Paginator的page_range属性,查询页面的范围,从1开始(>>> paginator.page_range)

 

2>.Page方法

>>> paginator.page(10)

>>>
>>> page = paginator.page(10)
>>>
>>> page.object_list
, , , , , , , , , , , <User: yinzhengjie
_187>, , , , , , , , ]>
>>>
Page的object_list属性,查询当前页面的对象列表(>>> page.object_list)
>>> page.number
10
>>>
Page的number属性,查询当前也的序号,从1开始(>>> page.number)
>>> page.paginator

>>>
Page的paginator属性,即获取到Paginator对象(>>> page.paginator)
>>> page.has_next()
True
>>>
Page的has_next()方法,判断是否还有下一页,如果有就返回True
>>> page.has_previous()
True
>>>
Page的has_previous()方法,如果有上一页,返回True(>>> page.has_previous())
>>> page.has_other_pages()
True
>>>
Page的has_other_pages()方法,如果有上一页或者下一页,返回True(>>> page.has_other_pages())
>>> page.next_page_number()
11
>>>
Page的next_page_number()方法,返回下一页的页码。如果不存在,抛出InvalidPage异常(>>> page.next_page_number())
>>> page.previous_page_number()
9
>>>
Page的previous_page_number()方法,返回上一页的页码。如果不存在,抛出InvalidPage异常(>>> page.previous_page_number())
>>> page.start_index()
181
>>>
Page的start_index()方法,返回当前页上的第一个对象,相对于分页列表的所有对象的序号(>>> page.start_index())
>>> page.end_index()
200
>>>
Page的.end_index()方法,返回当前页上的最后一个对象,相对于分页列表的所有对象的序号(>>> page.end_index())

 

转载于:https://www.cnblogs.com/yinzhengjie/p/10271694.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部