如何使分页与命令一起工作?

时间:2019-06-13 22:45:32

标签: python mysql flask

我想使用分页和按功能排序,但是效果不理想:

@app.route("/")
@app.route("/<type>")
@app.route('/page/<int:page>')
def index(type="top", page=0):
    perpage=5
    startat=page*perpage
    cur = mysql.connection.cursor()
    if type=='top':
        cur.execute("SELECT * FROM posts ORDER BY likes DESC LIMIT %s, %s", (startat,perpage))
        data = cur.fetchall()
        cur.close()
        return render_template("index.html", dat=data)
    elif type=='new':
        cur.execute("SELECT * FROM posts ORDER BY created_at DESC LIMIT %s, %s", (startat,perpage))
        data = cur.fetchall()
        cur.close()
        return render_template("index.html", dat=data)

但是它仅适用于“顶部”,而不适用于“新”,那么如何解决呢?

谢谢

0 个答案:

没有答案