python – Flask pagination without sqlAlchemy

Question:

I have a Flask application using a MySQL database. Raw MySQL queries. There is a page on which news are displayed. I want to do something like pagination or a button for dynamic loading, "Show more". If something is not clear written, do not get angry.

app.py
@app.route('/')
def index():
 
    return render_template('index.html', menu=dbase.getMenu(), posts=dbase.getPostAnonce())

getsql.py

class SqlCon():
    def __init__(self, bd):
        self.__bd = bd
        self.__cur =bd.cursor()
    def getPostAnonce(self):
        try:
            sql = "SELECT id, title, posttext, url, `data` FROM z250wzqrec95yxzh.postnews ORDER BY `data` 
         DESC "
            self.__cur.execute(sql)
            res = self.__cur.fetchall()
            if res: return res
        except pymysql.Error as e:
            print('Ошибка работы с таблицей sqlsite.postnews метод getPostAnonce'+str(e))
            return False
        return []

index.html

{% extends 'base.html' %}
{% block content %}
{{ super() }}

<h2>Новости охотничьего мира России</h2>
{% for p in posts %}
<div class="card">
  <div class="card-header">
   Дата: {{ p.data.strftime('%d-%m-%Y') }}
  </div>
  <div class="card-body">
    <h5 class="card-title">{{ p.title }}</h5>
    <p class="card-text">{{ p.posttext[:100]|striptags}}</p>
    <a href="{{url_for('showPost', allias=p.url)}}" class="btn btn-success">Читать новость</a>
  </div>
</div>
{% endfor %}


{% endblock content %}
{% block footer %}
{% endblock %}

Answer:

You can use url_for. In order to create a hyperlink to an object located in your project.

Scroll to Top