>>> for i in range(1,31):
... blog = Blog()
... blog.title = "for %s" % i
... blog.content = "这是for循环生成的第%s篇博客" % i
... blog.blog_type = blog_type
... blog.author = user
... blog.save()
...
...
>>> Blog.objects.all().count()
34
4.分页器实现分页
通过上图我们可以看出,当一下显示所有的博客时。因此实现分页功能,更便于我们查看想要浏览的博客。
>>> del Blog # 删除某个模型
>>> Blog
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'Blog' is not defined
>>> exit()
>>> python manage.py shell
>>> dir()
['__builtins__']
>>> from django.core.paginator import Paginator
>>> from blog.models import Blog
>>> Blog.objects.all()
<QuerySet [<Blog: <Blog: 长内容的博客>>, <Blog: <Blog: 人物介绍>>, <Blog: <Blog: Django简
介>>, <Blog: <Blog: shell模式下的第一篇博客>>, <Blog: <Blog: for 1>>, <Blog: <Blog: for 2>
>, <Blog: <Blog: for 3>>, <Blog: <Blog: for 4>>, <Blog: <Blog: for 5>>, <Blog: <Blog: for 6>>, <Blog: <Blog: for 7>>, <Blog: <Blog: for 8>>, <Blog: <Blog: for 9>>, <Blog: <Blog: for 10>>, <Blog: <Blog: for 11>>, <Blog: <Blog: for 12>>, <Blog: <Blog: for 13>>, <Blog: <Blog: for 14>>, <Blog: <Blog: for 15>>, <Blog: <Blog: for 16>>, '...(remaining elements truncated)...']>
>>> blogs = Blog.objects.all()
>>> blogs.count()
34
>>> paginator = Paginator(blogs, 10)
<string>:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'blog.models.Blog'> QuerySet.
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
# 创建博文类型模型
class BlogType(models.Model):
type_name = models.CharField(max_length=15)
def __str__(self):
return self.type_name
# 创建博文模型
class Blog(models.Model):
title = models.CharField(max_length=50)
# 一篇博文属于一种类型,因此博客通过外键关联到博文类型
blog_type = models.ForeignKey(BlogType, on_delete=models.DO_NOTHING)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
created_time = models.DateTimeField(auto_now_add=True)
last_updated_time = models.DateTimeField(auto_now=True)
def __str__(self):
return "<Blog: %s>" % self.title
<!-- 添加的新内容 -->
class Meta:
ordering = ['-created_time']
由于已经修改了Blog模型,所以要进行同步数据库。输入如下命令,如下图所示:
E:\Django\mysite_env\Scripts\mysite
$ python manage.py makemigrations
Migrations for 'blog':
blog\migrations\0003_auto_20200513_2224.py
- Change Meta options on blog
E:\Django\mysite_env\Scripts\mysite
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying blog.0003_auto_20200513_2224... OK
E:\Django\mysite_env\Scripts\mysite
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
May 13, 2020 - 22:24:47
Django version 2.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
退出之前的shell命令行模型,重新进入shell命令行模式下,输入如下代码:
>>> from django.core.paginator import Paginator
>>> from blog.models import Blog
>>> blogs = Blog.objects.all()
>>> paginator = Paginator(blogs, 10)
>>> paginator
<django.core.paginator.Paginator object at 0x000000000469D978>
>>> dir(paginator)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_object_list_is_ordered', '_get_page', 'allow_empty_first_page', 'count', 'get_page', 'num_pages', 'object_list', 'orphans', 'page', 'page_range', 'per_page', 'validate_number']
>>> paginator.__doc__
>>> paginator.count
34
>>> paginator.num_pages
4
>>> paginator.page_range
range(1, 5)
>>>
>>> page1 = paginator.page(1)
>>> page1
<Page 1 of 4>
>>> dir(page1)
['__abstractmethods__', '__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', 'count', 'end_index', 'has_next', 'has_other_pages', 'has_previous', 'index', 'next_page_number', 'number', 'object_list', 'paginator', 'previous_page_number', 'start_index']
>>> page1.count
<bound method Sequence.count of <Page 1 of 4>>
>>> help(page1.count)
Help on method count in module collections.abc:
count(value) method of django.core.paginator.Page instance
S.count(value) -> integer -- return number of occurrences of value
>>>
>>> page1.object_list
[<Blog: <Blog: for 30>>, <Blog: <Blog: for 29>>, <Blog: <Blog: for 28>>, <Blog: <Blog: for 27>>, <Blog: <Blog: for 26>>, <Blog: <Blog: for 25>>, <Blog: <Blog: for 24>>, <Blog: <Blog: for 23>>, <Blog: <Blog: for 22>>, <Blog: <Blog: for 21>>]
>>> page1.object_list.count
<built-in method count of list object at 0x00000000046AFDC8>