Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 330
Radu Avatar asked Apr 10 '17 21:04

Radu


People also ask

Can Flask handle multiple requests at the same time?

Such a different setup also means that they will handle concurrent requests differently. As of Flask 1.0, flask server is multi-threaded by default. Each new request is handled in a new thread.

How many requests can Flask handle at once?

For reference, the Flask benchmarks on techempower give 25,000 requests per second.

How do you handle a post request in Flask?

In order to demonstrate the use of POST method in URL routing, first let us create an HTML form and use the POST method to send form data to a URL. Now enter the following script in Python shell. After the development server starts running, open login. html in the browser, enter name in the text field and click Submit.

Can you use multiple decorators to route URLs to a function in Flask?

In some cases you can reuse a Flask route function for multiple URLs. Or you want the same page/response available via multiple URLs. In that case you can add a second route to the function by stacking a second route decorator to the function.


1 Answers

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)
like image 95
Radu Avatar answered Nov 11 '22 05:11

Radu