flast8 分頁

app

from flask import Flask
from flask_script import Manager

from App.extends import init_third
from App.views import bp

app = Flask(__name__)
app.config.from_pyfile("settings.py")
manager = Manager(app)
init_third(app)

# 註冊藍圖
app.register_blueprint(bp)

if __name__ == '__main__':
    manager.run()

settings

DEBUG = True
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:19970223@localhost:3306/py2"
SQLALCHEMY_TRACK_MODIFICATIONS = False

App.extends

from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap

db = SQLAlchemy()
bst = Bootstrap()

# 初始化第三方庫
def init_third(app):
    db.init_app(app=app)
    bst.init_app(app)

App.models

class User(db.Model):
    __tablename__ = 'user'

    uid = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(30), nullable=False)
    password = db.Column(db.String(128), nullable=False)
    gender = db.Column(db.Integer)

1

App.views

from flask import Blueprint, render_template
from App.models import User

bp = Blueprint("bp",__name__)


@bp.route("/list/")
@bp.route("/list/<int:page>/")
def list_user(page=1):
    # 搞清楚頁碼和記錄關係:limit (page-1)*10,10
    # 獲取分頁對象
    pagination = User.query.paginate(page,2)
    print(pagination.__dict__)
    #return render_template("list.html", **locals())
    return render_template("list1.html",**locals())

list.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1" cellspacing="0">
    {% for user in pagination.items %}
        <tr>
           <td>{{ user.username }}</td>
           <td>{{ user.password }}</td>
        </tr>
    {% endfor %}
</table>
<div>
    {% for current in pagination.iter_pages() %}
        <a href="{{ url_for('bp.list_user',page=current) }}">{{ current }}</a> &nbsp;&nbsp;
    {% endfor %}
</div>
</body>
</html>

list1.html

{% extends "bootstrap/base.html" %}

{% block content %}
<div class="bs-example" data-example-id="simple-table">
    <table class="table">
      <caption>Optional table caption.</caption>
      <thead>
        <tr>
          <th>Last Name</th>
          <th>Username</th>
        </tr>
      </thead>
      <tbody>
      {% for user in pagination.items %}
        <tr>
          <td>{{ user.username }}</td>
          <td>@{{ user.password }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
    {% from "page.html" import show_pagination %}
{{ show_pagination(pagination,'bp.list_user') }}
{% endblock %}

page.html

{% macro show_pagination(pagination, endpoint) %}
    <nav aria-label="Page navigation">
        <ul class="pagination">
            {# 上一頁 #}
            <li {% if not pagination.has_prev %}class="disabled"{% endif %}>
                <a href="{% if pagination.has_prev %}{{ url_for(endpoint, page=pagination.prev_num, **kwargs) }}{% else %}#{% endif %}" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>

            {# 中間頁碼 #}
            {% for p in pagination.iter_pages() %}
                {% if p %}
                    <li {% if pagination.page == p %}class="active"{% endif %}><a href="{{ url_for(endpoint, page=p, **kwargs) }}">{{ p }}</a></li>
                {% else %}
                    <li><a href="#">&hellip;</a></li>
                {% endif %}
            {% endfor %}

            {# 下一頁 #}
            <li {% if not pagination.has_next %}class="disabled"{% endif %}>
                <a href="{% if pagination.has_next %}{{ url_for(endpoint, page=pagination.next_num, **kwargs) }}{% else %}#{% endif %}" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        </ul>
    </nav>
{% endmacro %}

2

App.views

from flask import Blueprint, render_template
from App.models import User

bp = Blueprint("bp",__name__)

@bp.route("/list/")
@bp.route("/list/<int:page>/")
def list_user(page=1):
    # 參數:當前頁碼,每頁記錄個數
    pagination = User.query.paginate(page,1)
    # 頁碼少於5頁,全顯
    if pagination.pages <= 5:
        pagination.page_range = range(1,pagination.pages+1)
    # 如果多於5頁
    else:
        # 當前頁碼如果大於等於3並且page+2小於總頁數 [page-2,page+2]
        if page >= 3 and page+2 <= pagination.pages:
            pagination.page_range = range(page-2,page+3)
        # 當前頁碼如果大於等於3並且page+2大於總頁數
        elif page >= 3 and page+2 > pagination.pages:
            pagination.page_range = range(pagination.pages - 5, pagination.pages+1)
        # 如果頁碼小於3  [1,5]
        elif page < 3:
            pagination.page_range = range(1,6)
    return render_template('list.html',**locals())

list.html

{% extends "bootstrap/base.html" %}

{% block content %}
    <div class="bs-example" data-example-id="hoverable-table">
    <table class="table table-hover">
      <thead>
        <tr>
          <th>Last Name</th>
          <th>Username</th>
        </tr>
      </thead>
      <tbody>
      {% for user in pagination.items %}
        <tr>
          <td>{{ user.username }}</td>
          <td>{{ user.password }}</td>
        </tr>
      {% endfor %}
      </tbody>
    </table>
  </div>
    <nav aria-label="...">
      <ul class="pagination">
          {% if pagination.page == 1 %}
            <li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>
          {% else %}
              <li><a href="{{ url_for('bp.list_user',page=pagination.prev_num) }}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>
          {% endif %}
        {#  頁碼範圍    #}
          {% for current in pagination.page_range %}
              {% if current == pagination.page %}
                <li class="active"><a href="{{ url_for('bp.list_user',page=current) }}">{{ current }} <span class="sr-only">(current)</span></a></li>
              {% else %}
                  <li><a href="{{ url_for('bp.list_user',page=current) }}">{{ current }}</a></li>
              {% endif %}
          {% endfor %}

        {% if pagination.page == pagination.pages %}
            <li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>
        {% else %}
            <li><a href="{{ url_for('bp.list_user',page=pagination.next_num) }}" aria-label="Next"><span aria-hidden="true">»</span></a></li>

      {% endif %}
      </ul>
   </nav>
{% endblock %}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章