Python Django实现layui风格+django分页功能的例子

第一步:首先定义一个视图函数,用于提供数据,实现每页显示数据个数,返回每页请求数据

from django.shortcuts import render
from django.core.paginator import Paginator # Django内置分页功能模块

def index(request):
 # 提供json数据
 resp = {"id":10000,"username":"user-0","sex":"女","city":"城市-0","sign":"签名-0","experience":255,"logins":24,"wealth":82830700,"classify":"作家","score":57},{"id":10001,"username":"user-1","sex":"男","city":"城市-1","sign":"签名-1","experience":884,"logins":58,"wealth":64928690,"classify":"词人","score":27},{"id":10002,"username":"user-2","sex":"女","city":"城市-2","sign":"签名-2","experience":650,"logins":77,"wealth":6298078,"classify":"酱油","score":31},{"id":10003,"username":"user-3","sex":"女","city":"城市-3","sign":"签名-3","experience":362,"logins":157,"wealth":37117017,"classify":"诗人","score":68},{"id":10004,"username":"user-4","sex":"男","city":"城市-4","sign":"签名-4","experience":807,"logins":51,"wealth":76263262,"classify":"作家","score":6},{"id":10005,"username":"user-5","sex":"女","city":"城市-5","sign":"签名-5","experience":173,"logins":68,"wealth":60344147,"classify":"作家","score":87},{"id":10006,"username":"user-6","sex":"女","city":"城市-6","sign":"签名-6","experience":982,"logins":37,"wealth":57768166,"classify":"作家","score":34},{"id":10007,"username":"user-7","sex":"男","city":"城市-7","sign":"签名-7","experience":727,"logins":150,"wealth":82030578,"classify":"作家","score":28},{"id":10008,"username":"user-8","sex":"男","city":"城市-8","sign":"签名-8","experience":951,"logins":133,"wealth":16503371,"classify":"词人","score":14},{"id":10009,"username":"user-9","sex":"女","city":"城市-9","sign":"签名-9","experience":484,"logins":25,"wealth":86801934,"classify":"词人","score":75},{"id":10010,"username":"user-10","sex":"女","city":"城市-10","sign":"签名-10","experience":1016,"logins":182,"wealth":71294671,"classify":"诗人","score":34},{"id":10011,"username":"user-11","sex":"女","city":"城市-11","sign":"签名-11","experience":492,"logins":107,"wealth":8062783,"classify":"诗人","score":6},{"id":10012,"username":"user-12","sex":"女","city":"城市-12","sign":"签名-12","experience":106,"logins":176,"wealth":42622704,"classify":"词人","score":54},{"id":10013,"username":"user-13","sex":"男","city":"城市-13","sign":"签名-13","experience":1047,"logins":94,"wealth":59508583,"classify":"诗人","score":63},{"id":10014,"username":"user-14","sex":"男","city":"城市-14","sign":"签名-14","experience":873,"logins":116,"wealth":72549912,"classify":"词人","score":8},{"id":10015,"username":"user-15","sex":"女","city":"城市-15","sign":"签名-15","experience":1068,"logins":27,"wealth":52737025,"classify":"作家","score":28},{"id":10016,"username":"user-16","sex":"女","city":"城市-16","sign":"签名-16","experience":862,"logins":168,"wealth":37069775,"classify":"酱油","score":86},{"id":10017,"username":"user-17","sex":"女","city":"城市-17","sign":"签名-17","experience":1060,"logins":187,"wealth":66099525,"classify":"作家","score":69},{"id":10018,"username":"user-18","sex":"女","city":"城市-18","sign":"签名-18","experience":866,"logins":88,"wealth":81722326,"classify":"词人","score":74},{"id":10019,"username":"user-19","sex":"女","city":"城市-19","sign":"签名-19","experience":682,"logins":106,"wealth":68647362,"classify":"词人","score":51},{"id":10020,"username":"user-20","sex":"男","city":"城市-20","sign":"签名-20","experience":770,"logins":24,"wealth":92420248,"classify":"诗人","score":87},{"id":10021,"username":"user-21","sex":"男","city":"城市-21","sign":"签名-21","experience":184,"logins":131,"wealth":71566045,"classify":"词人","score":99},{"id":10022,"username":"user-22","sex":"男","city":"城市-22","sign":"签名-22","experience":739,"logins":152,"wealth":60907929,"classify":"作家","score":18},{"id":10023,"username":"user-23","sex":"女","city":"城市-23","sign":"签名-23","experience":127,"logins":82,"wealth":14765943,"classify":"作家","score":30},{"id":10024,"username":"user-24","sex":"女","city":"城市-24","sign":"签名-24","experience":212,"logins":133,"wealth":59011052,"classify":"词人","score":76},{"id":10025,"username":"user-25","sex":"女","city":"城市-25","sign":"签名-25","experience":938,"logins":182,"wealth":91183097,"classify":"作家","score":69},{"id":10026,"username":"user-26","sex":"男","city":"城市-26","sign":"签名-26","experience":978,"logins":7,"wealth":48008413,"classify":"作家","score":65},{"id":10027,"username":"user-27","sex":"女","city":"城市-27","sign":"签名-27","experience":371,"logins":44,"wealth":64419691,"classify":"诗人","score":60},{"id":10028,"username":"user-28","sex":"女","city":"城市-28","sign":"签名-28","experience":977,"logins":21,"wealth":75935022,"classify":"作家","score":37},{"id":10029,"username":"user-29","sex":"男","city":"城市-29","sign":"签名-29","experience":647,"logins":107,"wealth":97450636,"classify":"酱油","score":27}
 # 每页显示5条数据
 paginator = Paginator(resp, 5)
 page = request.GET.get('page')
 contacts = paginator.get_page(page)
 return render(request, 'index.html', {'contacts': contacts})

第二步:前端显示模块(基于layui模块),在templates目录下新建一个index.html文件(按照自己需求在官网复制代码)

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>翻页</title>
 <meta name="renderer" content="webkit">
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
 <link rel="stylesheet" href="../static/layui/css/layui.css" rel="external nofollow" media="all">
</head>
<body>
<div style="width: 650px; position: relative; left:25%;">

 <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px">
  <legend>翻页(layui风格+django功能)</legend>
 </fieldset>
  <table class="layui-table">
  <colgroup>
   <col width="150">
   <col width="200">
   <col>
  </colgroup>
  <thead>
   <tr class="layui-bg-red">
   <th>标题</th>
   <th>用户名</th>
   <th>手机号码</th>
   <th>邮箱地址</th>
   </tr>
  </thead>

  <tbody>
  {% for contact in contacts %}
  <tr>
   <td>{{ contact.id }}</td>
   <td>{{ contact.username }}</td>
   <td>{{ contact.sex }}</td>
   <td>{{ contact.experience }}</td>
   </tr>
  {% endfor %}
  </tbody>
  </table>

<script src="../static/layui/layui.js" charset="utf-8"></script>

</body>
</html>

第三步:在index.html里添加如下Django分页功能即可:

<div class="pagination">
 <span class="step-links">
  {% if contacts.has_previous %}
   <button class="layui-btn">
    <a href="?page=1" rel="external nofollow" >第一页</a>
   </button>

   <button class="layui-btn">
    <a href="?page={{ contacts.previous_page_number }}" rel="external nofollow" ><i class="layui-icon"></i></a>
   </button>
  {% endif %}

  <span class="current">
   <button class="layui-btn">
    第 {{ contacts.number }}页 共 {{ contacts.paginator.num_pages }}页
   </button>
  </span>

  {% if contacts.has_next %}
   <button class="layui-btn">
    <a href="?page={{ contacts.next_page_number }}" rel="external nofollow" ><i class="layui-icon"></i></a>
   </button>

   <button class="layui-btn">
    <a href="?page={{ contacts.paginator.num_pages }}" rel="external nofollow" >最后一页</a>
   </button>
  {% endif %}
 </span>
</div>
</div>

实现效果图如下:

以上这篇Python Django实现layui风格+django分页功能的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Python利用flask sqlalchemy实现分页效果

    Flask-sqlalchemy是关于flask一个针对数据库管理的.文中我们采用一个关于员工显示例子. 首先,我们创建SQLALCHEMY对像db. from flask import Flask, render_template,request from flask_sqlalchemy import SQLAlchemy app = Flask(__name__,static_url_path='') app.debug = True app.secret_key = "faefasdfa

  • python使用BeautifulSoup分页网页中超链接的方法

    本文实例讲述了python使用BeautifulSoup分页网页中超链接的方法.分享给大家供大家参考.具体如下: python通过BeautifulSoup分页网页中的超级链接,这段python代码输出www.jb51.net主页上所有包含了jb51的url链接 from BeautifulSoup import BeautifulSoup import urllib2 import re url = urllib2.urlopen("http://www.jb51.net") con

  • Python Django 简单分页的实现代码解析

    这篇文章主要介绍了Python Django 简单分页的实现代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 models.py: from django.db import models class Book(models.Model): title = models.CharField(max_length=32) def __str__(self): return self.title class Meta: db_table =

  • Python的Flask框架中实现分页功能的教程

    Blog Posts的提交 让我们从简单的开始.首页上必须有一张用户提交新的post的表单. 首先我们定义一个单域表单对象(fileapp/forms.py): class PostForm(Form): post = TextField('post', validators = [Required()]) 下面,我们把这个表单添加到template中(fileapp/templates/index.html): <!-- extend base layout --> {% extends &

  • python 实现分页显示从es中获取的数据方法

    注意:使用该方法,获取的数据总数目不能超过1万,否则出错 #在python3上运行 from elasticsearch import Elasticsearch from urllib3.connectionpool import xrange def get_page_data(result): for hit in result['hits']['hits']: print(hit) if __name__=='__main__': es_host = "0.0.0.0" por

  • python实现分页效果

    本文实例为大家分享了python实现分页效果展示的具体代码,供大家参考,具体内容如下 难点:清空Layout #!/usr/bin/python #-*-coding:utf-8-*- from PyQt4.QtCore import * from PyQt4.QtGui import * class PageWidget(QWidget): def __init__(self,parent=None): super(PageWidget,self).__init__(parent) self.

  • Python+Selenium自动化实现分页(pagination)处理

    场景 对分页来说,我们最感兴趣的是下面几个信息 总共有多少页 当前是第几页 是否可以上一页和下一页 代码 下面代码演示如何获取分页总数及当前页数.跳转到指定页数 #coding:utf-8 from selenium import webdriver import time driver = webdriver.Chrome() driver.get("https://segmentfault.com/news") # 获得所有分页的数量 # -2是因为要去掉上一个和下一个 total

  • python flask实现分页效果

    在我们学习的过程中会遇到这么样的问题,就是在我们学习的过程中会发现需要分页处理,这里呢,给大家介绍书上说的分页. @app.route('/',methods=['GET']) @app.route('/<int:page>') def home(page=1): pagination=Post.query.order_by(Post.publish_date.desc()).paginate(page, per_page=10,error_out=False) posts = paginat

  • python web框架中使用原生分页

    本文实例为大家分享了python web框架实现原生分页的具体代码,供大家参考,具体内容如下 原生分页器 示例 #!/usr/bin/env python # -*- coding:utf-8 -*- class Pagination: def __init__(self, p, all_count, pre=10, max_show=11): ''' :param p: 当前页码 :param all_count: 数据总条数 :param pre: 每页数据量 :param max_show

  • python flask实现分页的示例代码

    结合mysql数据库查询,实现分页效果 @user.route("/user_list",methods=['POST','GET']) def user_list(): p = g.args.get("p", '') #页数 show_shouye_status = 0 #显示首页状态 if p =='': p=1 else: p=int(p) if p > 1: show_shouye_status = 1 mdb = db_session() limit

  • Python Django 封装分页成通用的模块详解

    这篇文章主要介绍了Python Django 封装分页成通用的模块详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 新建 utils 文件夹,并创建 page.py page.py: class ShowPage(object): def __init__(self, page_num, total_count, url_prefix, per_page=10, max_page=11): ''' :param page_num: 当前页码数

  • 详解Python odoo中嵌入html简单的分页功能

    在odoo中,通过iframe嵌入 html,页面数据则通过controllers获取,使用jinja2模板传值渲染 html页面分页内容,这里写了判断逻辑 <!-- 分页 --> <ul id="ty_paging"> <li class="home" id="home"><a href="/car/budget/report/1" rel="external nofoll

随机推荐