Я предполагаю, что вы не используете ORM.
Простой пример
index.html
Go To Page
{% for i in range(1,5) %}
<a href="{{url_for('my_route', page = i)}}">{{i}}</a>
{% endfor %}
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/my_route')
def my_route():
page_no = int(request.args.get('page', 1))
results_per_page = 10
offset = (page_no-1) * results_per_page
my_db = 'mydb'
my_id = 1
query = """
SELECT [Id],[user], [post_text], [media_url], [media_type] FROM
[{}].[dbo].[SM_POSTS] WHERE Id = {}
ORDER BY [post_text] ASC
OFFSET {} ROWS
FETCH NEXT {} ROWS ONLY
""".format(my_db, my_id, offset, results_per_page)
# I am just returning the query itself
return query
if __name__ == "__main__":
app.run(debug=True)
Переход к http://127.0.0.1:5000/my_route?page=2
выдаст следующий запрос
SELECT [Id],[user], [post_text], [media_url], [media_type] FROM [mydb].[dbo].[SM_POSTS] WHERE Id = 1 ORDER BY [post_text] ASC OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY