I have written simple code that displays data from a SQLite database in a table. The table already contains pages to go through the entire contents of the database.
The problem is that right now the entire database is loaded into memory at once. How to make it split the query to the server into pages? The first part, i.e. loading and displaying a limited number of results when the page is first run, is not difficult:
list_of_games = list_of_games.order_by(GamesDatabase.date_added)[0:10]
But how to make the page send a query for the next results (e.g. 11-20) and display them only after pressing the button (e.g. "next page")? I want to create two separate buttons: "next page" and "previous page", which would be responsible for that.
I'm using Python with Flask (with SQLalchemy), SQLite.
PYTHON
@app.route("/test")
def test():
list_of_games = GamesDatabase.query
# print(request.args)
if "rating_min" in request.args:
rating_min = request.args.get("rating_min", 1, int)
list_of_games = list_of_games.filter(GamesDatabase.rating >= rating_min)
if "rating_max" in request.args:
rating_max = request.args.get("rating_max", 100, int)
list_of_games = list_of_games.filter(GamesDatabase.rating <= rating_max)
if "genre" in request.args:
list_of_games = list_of_games.filter(GamesDatabase.genre == request.args["genre"].upper())
list_of_games = list_of_games.order_by(GamesDatabase.date_added)
# args = []
# if request.args:
# for arg, val in request.args.items():
# args.append({"name": arg, "value": val})
return render_template("test.html", list_of_games=list_of_games) #args=args
HTML
{% extends "base.html" %}
{% block content %}
<head>
<title> Bootstrap SORT table Example </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.22/css/dataTables.bootstrap4.min.css">
<script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
<button class="btn btn-primary" onclick="document.location='{{url_for('index')}}'"style="margin-left: 790px;">go back</button>
<div class="container" style="width:60%">
<h2>all games </h2>
<table class="table table-striped table-bordered" id="sortTable">
<thead>
<tr>
<th>Name</th>
<th>Genre</th>
<th>Rating</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{% for game in list_of_games %}
<tr>
<td>{{ game.name}}</td>
<td>{{ game.genre }}</td>
<td>{{ game.rating }}%</td>
<td>${{ game.price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<form action="" method="GET">
{% for arg in args %}
<input type="hidden" name="{{ arg.name }}" value="{{ arg.value }} ">
{% endfor %}
<button type="submit">next_page </button>
</form>
<script>
$('#sortTable').DataTable();
</script>
</body>
<center>
<form action="" method="GET" >
<div class="input-group" style="width: 30%">
<div class="input-group-prepend">
<span class="input-group-text" id="rating">Rating</span>
</div>
<input type="number" id="rating_min" placeholder="min: 1" class="form-control" min="1" max="100" name="rating_min" />
<input type="number" id="rating_max" placeholder="max: 100" class="form-control" min="1" max="100" name="rating_max" />
</div>
<div class="input-group" style="width: 30%">
<div class="input-group-prepend">
<span class="input-group-text" id="genre_">Genre </span>
</div>
<input id="genre" placeholder="RPG" class="form-control" type="text" name="genre" required/>
</div>
<button class="btn btn-primary" onclick="document.location='{{url_for('test')}}'" style="margin-left: 0px;">submit</button>
</form>
</center>
<br>
{% endblock %}