python - 在 python flask 中使用游标对 postgres 查询进行分页

标签 python python-3.x postgresql flask cursor

我只想使用游标在 Flask 中创建分页,但我真的不知道该怎么做,因为我发现的其他解决方案非常复杂,而且我很难实现它。这里有人可以帮我吗?这是我的简单代码

@web_initials.route('/webpage/gmsi/list_of_users', defaults={'page': 1})
@web_initials.route('/webpage/gmsi/list_of_users/<page>')
@login_required
def list_of_users(page):

    conn2 = psycopg2.connect(database='mydb', user='myuser', host='myhost.host', password='mypassword')

    cur2 = conn2.cursor()
    cur2.execute('SELECT count(*) FROM tbl_users')
    x = [dict(((cur2.description[i][0]), value)
                    for i, value in enumerate(row)) for row in cur2.fetchall()]
    data2 = x[0]['count']
    conn = psycopg2.connect(database='mydb', user='myuser', host='myhost.host', password='mypassword')

    cur = conn.cursor()
    cur.execute('SELECT tbl_users.tbluserid, CONCAT(firstname,\' \', middle_initial, \' \', lastname) AS \"FULL NAME\", tbl_single_role.userrole, image_path, tbl_single_role.tblsingleroleid FROM tbl_users INNER JOIN tbl_single_role ON tbl_users.tblsingleroleid = tbl_single_role.tblsingleroleid  ORDER BY lastname ASC LIMIT {limit} offset {offset}'.format(limit = 5, offset = 0))
    data = cur.fetchall()
    page = request.args.get(get_page_parameter(), type=int, default=1)
    pagination = Pagination(page, total=data2, css_framework='bootstrap4', record_name='users')

    return render_template('tables.html', data = data, pagination=pagination)

这是我的html

{{ pagination.info }}
    {{ pagination.links }}
        <div class="table-responsive">

            <table class="table">
                <thead class=" text-primary">
                    <th>
                        Full Name
                    </th>
                        <th>
                          Photo
                        </th>

                    </thead>
                    <tbody>
                        {% for item in data %}
                    <tr>
                        <td>{{item[1]}}</td>
                                {% if item[3] == None %}
                                <td> <img class="img-fluid img-thumbnail" src="{{url_for('static', filename='assets/img/img.jpg')}}" id="imgfilechosen" height="60" width="60"/></td>
                            {% else %}
                        <td> <img class="img-fluid img-thumbnail" src="/{{item[3]}}" id="imgfilechosen" height="60" width="60"/></td>
                    {% endif %}
                </tr>
            {% endfor %}
        </tbody>
     </table>
{{ pagination.links }}

最佳答案

这是我解决自己问题的方法,很抱歉延迟:

类(class):

@web_initials.route('/webpage/gmsi/list_of_users', defaults={'page': 1})
@web_initials.route('/webpage/gmsi/list_of_users/<page>')
@login_required
def list_of_users(page):

    conn = psycopg2.connect(database='yourdatabase', user='youruser', host='yourhost', password='yourpassword')

    page = request.args.get(get_page_parameter(), type=int, default=int(page))
    if(page == 1):
        cur = conn.cursor()
        cur.execute('SELECT tbl_users.tbluserid, CONCAT(firstname,\' \', middle_initial, \' \', lastname) AS \"FULL NAME\", tbl_single_role.userrole, image_path, tbl_single_role.tblsingleroleid FROM tbl_users INNER JOIN tbl_single_role ON tbl_users.tblsingleroleid = tbl_single_role.tblsingleroleid  WHERE tbl_users.tblsingleroleid < 4 ORDER BY tbluserid ASC LIMIT {limit} offset {offset}'.format(limit = 10, offset = 0))
        data = cur.fetchall()
    else:
        cur = conn.cursor()
        cur.execute('SELECT tbl_users.tbluserid, CONCAT(firstname,\' \', middle_initial, \' \', lastname) AS \"FULL NAME\", tbl_single_role.userrole, image_path, tbl_single_role.tblsingleroleid FROM tbl_users INNER JOIN tbl_single_role ON tbl_users.tblsingleroleid = tbl_single_role.tblsingleroleid  WHERE tbl_users.tblsingleroleid < 4 ORDER BY tbluserid ASC LIMIT {limit} offset {offset}'.format(limit = 10, offset = (5 * int(page))))
        data = cur.fetchall()


    pagination = Pagination(page=page, total=data2, css_framework='bootstrap4', record_name='users')

    return render_template('tables.html', data = data, pagination=pagination)

html 端:

  <div class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-md-12">
          <div class="card">
            <div class="card-header card-header-success text-white bg-dark">
              <h4 class="card-title ">List of Users</h4>
              <p class="card-category"></p>
            </div>
            <div class="card-body">
            {{ pagination.info }}
              {{ pagination.links }}
              <div class="table-responsive">

                <table class="table">
                  <thead class="bg-primary">
                    <th>
                      Full Name
                    </th>
                    <th>
                    <center>
                      Photo
                      </center>
                    </th>
                    <th>
                    <center>
                      Last Location
                      </center>
                    </th>
                     <th>
                     <center>

                      </center>
                    </th>
                  </thead>
                  <tbody>
                       {% for item in data %}
                        <tr>
                            <td>{{item[1]}}</td>
                            {% if item[3] == None %}
                            <td><center> <img class="img-fluid img-thumbnail" src="{{url_for('static', filename='assets/img/img.jpg')}}" id="imgfilechosen" height="60" width="60"/></center></td>
                              {% else %}
                              <td> <center><img class="img-fluid img-thumbnail" src="/{{item[3]}}" id="imgfilechosen" height="60" width="60"/></center></td>
                            {% endif %}
                            <td><center><a href="{{ url_for('web_initials.merchandiser_location',  id = item[0]) }}" target="_blank">View</a></center></td>

                            <td><center><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">EDIT</button></center></td>
                          </tr>
                        {% endfor %}
                  </tbody>
                </table>
                {{ pagination.links }}
              </div>
            </div>
          </div>
        </div>

关于python - 在 python flask 中使用游标对 postgres 查询进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53969159/

相关文章:

mysql - 跨多个异构数据库执行连接,例如PostgreSQL 和 MySQL

python - 如何在 Python 中将彩色输出打印到终端?

python - 如何去掉某个字符串后的所有内容

python - 如何修复重新声明的警告?

python - 当我尝试向 defaultdict(str) 添加值时出现错误

postgresql - 表在 postgres openerp 中最多可以有 1600 列

python - 如何使用选定的模块有条件地构建 python 包

python - Django抽象模型继承

python - 随机 DNA 突变发生器

sql比较具有不同参数的条目