【问题标题】:How to implement pagination in mysql如何在mysql中实现分页
【发布时间】:2021-07-11 16:54:36
【问题描述】:

我正在尝试实现分页,我找到了可行的解决方案,但在 SQLLite 中,我想要一个 MySQL 数据库的解决方案。我正在努力添加Employees.query.paginate,因为它在 MySQL 数据库中根本不起作用。我没有可以调用的类,所以我的问题是如何省略它以及哪段代码将起作用?我在 Google 中浏览了多个页面,但没有找到任何可行的方法(或者显然我无法正确实现它)。

app.py 取自here

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
app.config['SECRET_KEY'] = 'secret key'

db = SQLAlchemy(app)


class Employees(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    fullname = db.Column(db.String(150))
    position = db.Column(db.String(150))
    office = db.Column(db.String(150))
    age = db.Column(db.Integer)
    startdate = db.Column(db.String(150))
    salary = db.Column(db.String(150))



@app.route('/employees/<int:page_num>')
def employees(page_num):
    employees = Employees.query.paginate(per_page=2, page=page_num, error_out=True)
    return render_template('index.html', employees=employees)


if __name__ == '__main__':
    app.run(debug=True)

index.html 取自here

    <html>
    <head>
    <title>Python Flask SQLAlchemy Pagination</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
    <div class="container">
     <div class="row">
     <p><h2>Python Flask SQLAlchemy Pagination - Datatable bootstap</h2>  </p>
     <table id="example" class="table table-striped table-bordered" style="width:100%">
            <thead>
                <tr>
                    <th><input type="checkbox" onclick="checkAll(this)"></th>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Age</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
       {% for employee in employees.items %}
       <tr>
                    <td><input type="checkbox" name=""></td>
                    <td>{{ employee.fullname}}</td>
                    <td>{{ employee.position}}</td>
                    <td>{{ employee.office}}</td>
                    <td>{{ employee.age}}</td>
                    <td>{{ employee.startdate}}</td>
                    <td>{{ employee.salary}}</td>
                </tr>
                {% endfor %}
            </tbody>
            <tfoot>
                <tr>
                    <th></th>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Age</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </tfoot>
        </table>
     </div>
    
       <ul class="pagination">
      {% if employees.has_prev %}
       <li class="page-item"><a class="page-link" href="{{ url_for('employees', page_num=employees.prev_num) }}">Previous</a></li>
      {% else %}
       <li class="page-item disabled"><span class="page-link">Previous</span>
      {% endif %}
       </li>
    
      {% for page in employees.iter_pages(left_edge=3, right_edge=3) %}
      {% if page %}
       <li class="page-item"><a class="page-link" href="{{ url_for('employees', page_num=page) }}">{{ page }}</a></li>
      {% else %}
       <li class="page-item disabled" id="example_ellipsis"><a href="#" class="page-link">…</a></li>
      {% endif %}
      {% endfor %}
    
      {% if employees.has_next %}
       <li class="page-item"><a class="page-link" href="{{ url_for('employees', page_num=employees.next_num) }}">Next</a></li>
      {% else %}
       <li class="page-item disabled"><span class="page-link">Next</span>
      {% endif %}
       </ul>
    
    
    </div>
    <style>
    table{
        width:100%;
    }
    #example_filter{
        float:right;
    }
    #example_paginate{
        float:right;
    }
    label {
        display: inline-flex;
        margin-bottom: .5rem;
        margin-top: .5rem;
    
    }
    .page-item.disabled .page-link {
        color: #6c757d;
        pointer-events: none;
        cursor: auto;
        background-color: #fff;
        border-color: #dee2e6;
    }
    </style>
    <script>
    function checkAll(bx) {
      var cbs = document.getElementsByTagName('input');
      for(var i=0; i < cbs.length; i++) {
        if(cbs[i].type == 'checkbox') {
     

 cbs[i].checked = bx.checked;
    }
  }
}
</script>
</body>
</html>

您能否建议如何在我的代码中实现类似的东西:

@app.route('/content_repository')
def content_repository():
    # Check if user is loggedin
    if 'loggedin' in session:

        # User is loggedin show them the home page
        cursor = connection.cursor(buffered=True)
        cursor.execute("USE content_repository")
        cursor.execute("""SELECT *  FROM product""")

        cursor = connection.cursor(buffered=True, dictionary=False)
        cursor.execute("USE content_repository")
        cursor.execute("""SELECT * FROM product""")

        connection.commit()
        data = cursor.fetchall()

        return render_template('index.html', username=session['username'], data=data)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))

我的 index.html 文件

<div id="content">
    <div class="container-fluid mx-auto pt-4">
        <table class="table table-striped table-bordered" id="TableToExport" cellspacing="0">
            <thead>
            <tr>
                <th style="width: 9%">Date</th>
                <th style="width: 7%">Code</th>
                <th style="width: 9%">Project Name</th>
                <th style="width: 7%">Country</th>
                <th style="width: 5%">Eretailer</th>
                <th style="width: 7%">Brand</th>
                <th style="width: 7%">Category</th>
                <th style="width: 9%">Title</th>
                <th style="width: 15%">Feature</th>
                <th style="width: 20%">Description</th>
                <th style="width: 3%">Version</th>
            </tr>
            </thead>

            <tbody>
                {% for item in data %}
                    <tr>
                        <td>{{item[0]}}</td>
                        <td>{{item[1]}}</td>
                        <td>{{item[2]}}</td>
                        <td>{{item[3]}}</td>
                        <td>{{item[4]}}</td>
                        <td>{{item[5]}}</td>
                        <td>{{item[6]}}</td>
                        <td>{{item[7]}}</td>
                        <td>{{item[8]}}</td>
                        <td>{{item[9]}}</td>
                        <td>{{item[10]}}</td>
                    </tr>

                {% endfor %}
        </table>
    </div>
</div>

【问题讨论】:

    标签: python mysql flask pagination


    【解决方案1】:

    您创建的代码不使用 orm 技术 (SQLALCHEMY)

    您无法关注示例分页,因为它加载了 ORM

    【讨论】:

      猜你喜欢
      • 2019-01-06
      • 2016-07-23
      • 1970-01-01
      • 2011-02-25
      • 2017-03-07
      • 2021-11-25
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多