[py][mx]django分页第三方模块django-pure-pagination

前台的这些数据都是从后台取来的

分页模块django-pure-pagination

- 一款基于django pagination封装的更好用的分页模块
https://github.com/jamespacileo/django-pure-pagination

- 安装
pip install django-pure-pagination

views.py

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

class OrgView(View):  # 课程机构列表页
    def get(self, request):
        all_orgs = CourseOrg.objects.all()  # 所有课程机构
        org_count = all_orgs.count()  # 多少家课程机构
        all_citys = CityDict.objects.all()  # 所有城市列表
        
        # 分页模块
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(all_orgs, 3, request=request)
        orgs = p.page(page)

        return render(request, 'org-list.html', {
            "all_orgs": orgs,
            "all_citys": all_citys,
            'org_count': org_count,
        })

设置每页显示

前端修改

注: 这里org_count显示多少家, all_orgs.count()

class OrgView(View):  # 课程机构列表页
    def get(self, request):
        all_orgs = CourseOrg.objects.all()  # 所有课程机构
        org_count = all_orgs.count()  #显示多少家课程机构
    {#    课程机构    #}
    <div>
        <strong>共{{ org_count }}家<strong/>
            <ul>
                {% for course_org in all_orgs.object_list %}
                    <li><img src="{{ MEDIA_URL }}{{ course_org.image }}" alt=""></li>
                {% endfor %}
            </ul>

            {#  渲染默认样式的分页 #}
            <p>{{ all_orgs.render }}</p> 
    </div>

完整的前端代码

{% extends 'base.html' %}{# 一定要出现在第一行 #}
{% load staticfiles %}
{% block title %}
    课程列表
{% endblock %}

{% block custom_bread %}
    <div>
        <ul>
            <li><a href="">首页</a>>课程机构</li>

        </ul>
    </div>
{% endblock %}

{% block content %}
    {#    机构类别    #}
    <div>
        <p><strong>机构类别</strong>: 全部 培训结构 高校 个人</p>
    </div>

    {#    城市    #}
    <div>
        <p><strong>城市</strong>:{% for city in all_citys %}
            {{ city.name }}
        {% endfor %}
        </p>
    </div>
    {#    课程机构    #}
    <div>
        <strong>共{{ org_count }}家<strong/>
            <ul>
                {% for course_org in all_orgs.object_list %}
                    <li><img src="{{ MEDIA_URL }}{{ course_org.image }}" alt=""></li>
                {% endfor %}
            </ul>
            <p>{{ all_orgs.render }}</p>
    </div>
{% endblock %}

posted @ 2018-02-04 11:41  mmaotai  阅读(218)  评论(0编辑  收藏  举报