[Python] How to refresh items on a page without page refresh in Flask

Discussão em 'Python' iniciado por Stack, Agosto 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to add a script which directs me to the same page with refreshed content.

    I'm using an sqlite3 database for my code and when i try it only works with the page refresh. This is one of my SQL structures.

    CREATE TABLE classes (
    model_id TEXT NOT NULL,
    class_number INTEGER,
    class_name TEXT,
    FOREIGN KEY(model_id) REFERENCES models(model_id)
    );


    The problem is this is my flask route i use this kind of structure:

    @app.route("/model/<model_id>",methods=["POST","GET"])
    @login_required
    def view_model(model_id):
    ...
    with sqlite3.connect("database.db") as con:
    .
    .
    .
    return render_template("train.html",
    categories=CATEGORIES,
    model_id=model_id,
    model=model,
    classes=classes,
    images=images,
    istrained=istrained,
    test_image=test_image
    )
    images=images)


    And when ijust need to change the SQL just like in this route i need to either redirect(f"/model/{model_id}") or return "",402.In the first case all the page reloads and since the sql change it successfully reloads all the page.Also when i try the second one nothing applies since the classes is defined in the first route.

    @app.route("/add_class/<model_id>",methods=["POST","GET"])
    @login_required
    def add_class(model_id):
    with sqlite3.connect("database.db") as con:
    con.row_factory = sqlite3.Row
    db = con.cursor()
    result = db.execute("SELECT MAX(class_number) FROM classes WHERE model_id = ?", (model_id,)).fetchone()
    max_num = result[0] if result and result[0] is not None else 0
    default_num = int(max_num +1)
    default_name = "Class " + str(max_num + 1)
    db.execute("INSERT INTO classes (model_id,class_name,class_number) VALUES(?,?,?)",
    (model_id,default_name,default_num))
    con.commit()
    return redirect(f"/model/{model_id}")


    I need to find a way to change the content but without all the page reloading.And want to know are there any ways to solve this problem without javascript.

    Continue reading...

Compartilhe esta Página