【问题标题】:Pagination of postgres query using cursor in python flask在python烧瓶中使用游标对postgres查询进行分页
【发布时间】:2018-12-29 11:36:13
【问题描述】:

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

@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 }}

【问题讨论】:

  • 我已经自己解决了。

标签: python python-3.x postgresql flask cursor


【解决方案1】:

以下是我自己解决问题的方法,抱歉耽搁了:

类:

@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>

【讨论】:

    【解决方案2】:

    我刚刚解决了它,我想出了如何用这些行来制作它,不需要在 github 中实现那些长方法。

    【讨论】:

    • 我为您感到高兴,但很高兴分享您的解决方案,以便其他可能遇到您问题的人得到帮助。
    • 我没有在这里发布我的工作代码,因为有成千上万的地方可以制作长方法。我只是一个非常菜鸟,python 语言的新手,这就是为什么我很难实现它,我认为这只是浪费时间,我在这里的大部分问题都是由我自己回答的。我不知道他们为什么不能或者他们只是不想回答。这就是我停止在这里发布问题的原因。无论如何,感谢您的投票。
    • 您求助并在 4 小时内自行解决了问题。这是非常好的。帮助可能需要比这更长的时间。您不会立即从某人那里得到解决方案。这里没有人付钱来解决别人的问题,每个人都只是想帮忙。你也应该这样做,不要抱怨其他人缺乏兴趣或我的反对意见。 Downvote 意味着答案没有为问题提供任何有用的信息。
    • 好的,我发布我的解决方案。非常感谢您的好建议先生。
    • @NoahMartin 先生,晚上好,我已经发布了我非常简单的修复,对不起,我是一个脾气暴躁的人。非常感谢先生,愿上帝永远保佑您。 :)
    猜你喜欢
    • 2020-02-05
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2012-08-19
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多