**python code:**
from flask import Flask, render_template, request, redirect, url_for, session ,flash
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re
app = Flask(__name__)
app.secret_key = 'your secret key'
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'details'
mysql = MySQL(app)
@app.route('/home')
def home():
if 'loggedin' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM ml_process_status")
data = cur.fetchall()
return render_template('home.html', username=session['username'], items=data)
return redirect(url_for('login'))
There are 50 rows in phpmyadmin mysql database. Is there any solution to show only 10 rows in first page and next 10 rows in other pages. I have select * query to show all the rows in the page
CodePudding user response:
To do this you have to pass a parameter to your endpoint (which indicates the page to be displayed), and to select the range of records concerned by using OFFSET AND LIMIT inside your sql statement.
Example:
@app.route('/home')
def home():
if 'loggedin' in session:
page = request.args.get('page', 1)
elem_to_display = 10
offset = (page - 1) * elem_to_display
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM ml_process_status LIMIT %s OFFSET %s", (elem_to_display, offset,))
data = cur.fetchall()
return render_template('home.html', username=session['username'], items=data)
return redirect(url_for('login'))