1. 程式人生 > >Django2.0:【Django2.0教程】13.分頁和shell命令列模式 視訊學習筆記

Django2.0:【Django2.0教程】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)

分頁的使用

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">&laquo;</span>
                                </a>
                            {% else %}
                                <span aria-hidden="true">&laquo;</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">&raquo;</span>
                            </a>
                            {% else %}
                                <span aria-hidden="true">&raquo;</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 %}