视频链接:13.分页和shell命令行模式
快速添加博文:Shell命令行模式
$ python manage.py shell
导入Blog模型:
>>> from blog.models import Blog
验证是否成功引用:
>>> dir()
查看所有博文:
>>> Blog.objects.all()
查看博文数量:
>>> Blog.objects.count()
或者
>>> Blog.objects.all().count()
模型新增对象
实例化对象:>>> blog = Blog()
查看是否成功:
>>> dir()
因为库中没有,所以库中查不到:
>>> Blog.objects.all()
添加文章标题:
>>> blog.title = "shell下第一篇"
添加文章内容:
>>> blog.content = "xxxxxxxxxxxxxx"
引入BlogType:
>>> from blog.models import BlogType
查询所有BlogType:
>>> BlogType.objects.all()
添加文章类型:
>>> blog.blog_type = BlogType.objects.all()[0]
引入Django的User模型:
>>> from django.contrib.auth.models import User
查询所有User:
>>> User.objects.all()
添加作者:
>>> blog.author = User.objects.all()[0]
保存入库:
>>> blog.save()
查询库:
>>> Blog.objects.all()
查询blog对象的方法:
>>> dir(blog)
查询最新修改时间:
>>> blog.last_updated_time
添加大量文章:
>>> for i in range(1, 31):
... blog = Blog()
... blog.title = "for %s" % i
... blog.content = "xxxx: %s" % i
... blog.blog_type = BlogType.objects.all()[0]
... blog.author = User.objects.all()[0]
... blog.save()
查看是否成功新增:
>>> Blog.objects.all().count()
导入分页器:
>>> from django.core.paginator import Paginator
查看是否成功引入:
>>> dir()
有些不需要的可以移除,比如:
>>> del Blog
获取全部博文:
>>> blogs = Blog.objects.all()
实例化分页器:
>>> paginator = Paginator(blogs, 10) # 第一个参数是具体的对象列表,第二个参数是每页文章数
模型有默认的排序,但不知道是不是每页的内容不一样,即第一篇博文可能在这一页出现,同时在另一页也出现。所以,最好有个排序规则。按照这个排序规则分页。
...
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'] # 按照created_time倒序排序
$ python manage.py makemigrations
$ python manage.py migrate
重新打开shell,引入需要的包(Blog、Paginator),再次实例化分页器:
>>> paginator = Paginator(blogs, 10)
可以打印出paginator:
>>> paginator
查看pagnitor有什么属性和方法:
>>> dir(paginator)
查看有多少篇文章:
>>> paginator.count
查看有多少页:
>>> paginator.num_pages
查看页码:
>>> paginator.pag_range()
取第一页:
>>> page1 = paginator.page(1)
查看page1的属性和方法,通过help获取某方法的使用说明:
>>> help(page1.count)
分页的使用
通过GET方法获取请求参数,例如: http://127.0.0.1:8000/blog/?page=2
修改blog/views.py:
from django.shortcuts import render_to_response, get_object_or_404
from django.core.paginator import Paginator
from .models import Blog, BlogType
def blog_list(request):
blogs_all_list = Blog.objects.all()
paginator = Paginator(blogs_all_list, 10)
page_num = request.GET.get('page', 1) # 获取url的页码参数。GET返回字典,page_num默认为1
page_of_blogs = paginator.get_page(page_num)
context = {}
context['page_of_blogs'] = page_of_blogs
# context['blogs_count'] = Blog.objects.all().count()
context['blog_types'] = BlogType.objects.all()
return render_to_response('blog/blog_list.html', context)
...
修改blog/templates/blog/blog_list.html:
...
{% block content %}
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-9 col-lg-10">
<div class="panel panel-default">
...
</div>
<div>
<ul class="pagination">
<li>
{# 上一页 #}
{% if page_of_blogs.has_previous %}
<a href="?page={{ page_of_blogs.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
{% else %}
<span aria-hidden="true">«</span>
{% endif %}
</li>
<li>
{# 页码 #}
{% for page_num in page_of_blogs.paginator.page_range %}
<a href="?page={{ page_num }}">{{ page_num }}</a>
{% endfor %}
</li>
<li>
{# 下一页 #}
{% if page_of_blogs.has_next %}
<a href="?page={{ page_of_blogs.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
{% else %}
<span aria-hidden="true">»</span>
{% endif %}
</li>
</ul>
</div>
</div>
<div class="hidden-xs col-sm-4 col-md-3 col-lg-2">
<div class="panel panel-default">
<div class="panel-heading">博客分类</div>
<div class="panel-body">
<ul class="blog-types">
{% for blog_type in blog_types %}
<li>
<a href="{% url 'blogs_with_type' blog_type.pk %}">{{ blog_type.type_name }}</a>
</li>
{% empty %}
<li>暂无分类</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</div>
</div>
{% endblock %}