如何在Flask中跨多页显示词典?

2024-10-01 07:39:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据库中有1000多个项目,我想在我的网站上显示这些产品。这些条目作为字典获取。我在StackOverflow上搜索了这个问题。我找到了一些东西,但这些东西是作为一个列表提取的

@app.route("/",methods = ["GET","POST"])
def index():
    if request.method == "GET":
        cursor = mysql.connection.cursor()
        sorgu = "SELECT * from all_products"
        result = cursor.execute(sorgu)
        if result > 0:
            products = cursor.fetchall()
            totalproducts = len(products)
            totalproductsinpage = countproductsall(products)
            success = int((totalproducts - totalproductsinpage )* 100 / totalproducts)
            return render_template("index.html",products = products,totalproducts = totalproducts,totalproductsinpage = totalproductsinpage,success = success)
        else:
            flash("Ürün bulunamadı!","warning")
            return render_template("index.html")

Tags: 数据库getindexreturnifhtmltemplateresult