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)
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.
For reference, the Flask benchmarks on techempower give 25,000 requests per second.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With