自制django分页安装包安装及使用说明


发布时间:2020-01-05 13:15    作者: 晖哥哥   已过去:2 年   阅读总量:1263 已被赞:0


本分页安装包适合django2.0和ptyhon3以上

效果:

使用步骤:

安装:


pip install huigege_page_views

使用:

1.在views.py 引入

from huigege_page_views.page_views import Huigege_page_views

2.新建一个类视图,继承Huigege_page_views,根据需求修改下面传入的参数就可以

class Page_hui(Huigege_page_views):
    model = News  # 数据表,分页数据来源的表模型名
    template_name = 'news_new_list.html'  # 前段展示数据的html模板
    context_object_name = 'newsCategorys'  # 前段获取分页数据进行遍历的变量名
    paginate_by = 2  # 按多少条数据分一页
    ordering = 'c_time'  # 按什么字段排序
    page_kwarg = 'p'  # url上?后的截取字符,如:127.0.0.1:8000/news?p=1

3.建立url 连接

例如:

path('category_list/',views.Categorys_hui.as_view(),name='category_list'),

4.html页面:

遍历的变量要和3步的一致,比如:newsCategorys

例如:

{% for newsCategory in newsCategorys %}
    <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ newsCategory.name }}</td>
        <td>{{ newsCategory.ms }}</td>
        <td>{{ newsCategory.c_time}}</td>
{% endfor %}

分页代码,直接拷贝在底部使用:

 {#    分页#}
      <div class="page">
          <div>
                {#上一页#}
                {% if page_obj.has_previous %}
                    <a class="prev" href="{% url 'news:category_list' %}?p={{ page_obj.previous_page_number }}"><<</a>
                {% else %}
                    <a class="prev" href="javascript:void(0);" style="cursor: not-allowed"><<</a>
                {% endif %}
                {% if left_has_more %}
                    <a class="prev" href="{% url 'news:category_list' %}?p=1">1</a>
                    <a class="prev" href="javascript:void(0);">...</a>
                {% endif %}
                {# 左边的页码 #}
                {% for left_page in left_pages %}
                    <a class="num" href="{% url 'news:category_list' %}?p={{ left_page }}">{{ left_page }}</a>
                {% endfor %}
                {# 当前的页码#}
                <span class="current">{{ current_page }}</span>
                {#<a href="{% url 'menu_manage:menu_list' %}?p={{ current_page }}" class="current">{{ current_page }}</a>#}
                {# 右边的页码 #}
                {% for right_page in right_pages %}
                    <a class="num" href="{% url 'news:category_list' %}?p={{ right_page }}">{{ right_page }}</a>
                {% endfor %}
                {% if right_has_more %}
                    <a class="num" href="javascript:void(0);">...</a>
                    <a class="num" href="{% url 'news:category_list' %}?p={{ num_pages }}">{{ num_pages }}</a>
                {% endif %}
                {#下一页#}
                {% if page_obj.has_next %}
                    <a class="next" href="{% url 'news:category_list' %}?p={{ page_obj.next_page_number }}">&gt;&gt;</a>
                {% else %}
                    <a class="disabled next" href="javascript:void(0);" style="cursor: not-allowed">&gt;&gt;</a>
                {% endif %}
            </div>
      </div>
{#分页结束#}

结束

点赞

0




登陆后方可评论