'Different types of POST requests in the same route in Flask

I want to have a page on my website where you have multiple buttons that send a different POST request to modify some part of my database.

Currently, only the top if statement gets executed. If I try the two bottom ones, I get:The browser (or proxy) sent a request that this server could not understand.

If I switch them around, it is always the top if statement that gets executed.

Am I doing this wrong? Is there a better way to do this kind of thing?

@app.route('/', methods=["GET", "POST"])
@login_required
def homepage():
    if request.method == "POST" and request.form['estimatedCost']:
        _projectName = request.form['projectName']
        _estimatedCost = request.form['estimatedCost']
        _amountAllocated = request.form['amountAllocated']
        conn, cursor = connectDB()
        cursor.execute("INSERT INTO `project` (`name`, `estimatedCost`, `amountAllocated`, `pStatus`, `actualCost`, `estimatedDuration`, `actualDuration`, `costDifference`) VALUES ( '" + _projectName + "', '" + _estimatedCost + "', '" + _amountAllocated + "', 'NOT STARTED', 0, 0, 0, NULL)")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and request.form['delete']:
        _delete = request.form['delete']
        conn, cursor = connectDB()
        cursor.execute("DELETE FROM project WHERE name = '" + _delete + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and request.form['pid']:
        _pid = request.form['pid']
        _status = request.form['status']
        conn, cursor = connectDB()
        cursor.execute("UPDATE project SET pStatus = '" + _status + "' WHERE name = '" + _pid + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    conn, cursor = connectDB()
    cursor.execute("SELECT * FROM project")
    projects = cursor.fetchall()
    conn.close()
    return render_template("dashboard.html", projectDic = projects)


Solution 1:[1]

I managed to find a solution for my problem.

Because request.form['key'] was causing an error if it didn't exist, instead of just become False, it was making the page to crash.

Instead, I used "key" in request.form to check if that input was filled in the form.

Here is the corrected code:

@app.route('/', methods=["GET", "POST"])
@login_required
def homepage():
    if request.method == "POST" and "estimatedCost" in request.form:
        _projectName = request.form['projectName']
        _estimatedCost = request.form['estimatedCost']
        _amountAllocated = request.form['amountAllocated']
        conn, cursor = connectDB()
        cursor.execute("INSERT INTO `project` (`name`, `estimatedCost`, `amountAllocated`, `pStatus`, `actualCost`, `estimatedDuration`, `actualDuration`, `costDifference`) VALUES ( '" + _projectName + "', '" + _estimatedCost + "', '" + _amountAllocated + "', 'NOT STARTED', 0, 0, 0, NULL)")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and "delete" in request.form:
        _delete = request.form['delete']
        conn, cursor = connectDB()
        cursor.execute("DELETE FROM project WHERE name = '" + _delete + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    if request.method == "POST" and "pid" in request.form:
        _pid = request.form['pid']
        _status = request.form['status']
        conn, cursor = connectDB()
        cursor.execute("UPDATE project SET pStatus = '" + _status + "' WHERE name = '" + _pid + "'")
        conn.commit()
        conn.close()
        return redirect('/')
    conn, cursor = connectDB()
    cursor.execute("SELECT * FROM project")
    projects = cursor.fetchall()
    conn.close()
    return render_template("dashboard.html", projectDic = projects)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Radu