I just want to create pagination in flask using cursor, but i really don't know how to do it since the other solutions that i found were very complicated and it's hard for me to implement it. can anyone here help me with this? here's my simple code
@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)
here is my 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 }}
Here's how i solved my own problem sorry for the delay:
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 side:
<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>