Skip to content

Delete a task stored in SQL database through html form using Python

This is a web app written in Python to add and delete tasks.

This is the add_tasks function:

@app.route("/add", methods=["GET", "POST"])
def add():
    if request.method == "POST":                
        add = request.form.get("add")
        quantity = request.form.get("quantity")
        if not add:
            return apology("Please add an item")      
        db.execute("INSERT INTO history (add, quantity, user_id) VALUES (:add, :quantity, :user_id);", add=add, quantity=quantity, user_id=session["user_id"])

        return redirect("/")

        return render_template("add.html")

Then my delete function takes only the stored items from SQL database to SELECT, in order to erase them.

@app.route("/delete", methods=["GET", "POST"])
def delete():

    if request.method == "POST":
        db.execute("DELETE FROM history (add, quantity, user_id) VALUES (:add, :quantity, :user_id);", add=add, quantity=0, user_id=session["user_id"])

        rows = db.execute("SELECT add FROM history WHERE user_id=:user_id GROUP BY add",
        return render_template("delete.html", add=[row["add"] for row in rows])

This delete function leads to an internal server error. What´s wrong about it?



Finally, the POST part of my DELETE function was wrong. I´ve managed to find this solution:

db.execute("DELETE FROM history WHERE user_id=:user_id and add=:add;", user_id=session["user_id"], add=add)

Thanks, RobinFrcd for your help!