I thought I sort of understood GET and POST requests in that:
- GET request means the user only retrieves data from the server; cannot change anything like the database
- POST request means the user can send data to the server and change something
As a final touch, I decided to create a route called /account (added to the navbar next to Log Out) where user can input their old pw, new pw, confirm pw. Then if old pw is in database, they update with new pw.
I set it up like this:
@app.route("/account", methods=["GET", "POST"])
@login_required
def account():
if request.method == "POST":
# update pw
return redirect(url_for("index"))
else:
return redirect(url_for("index"))
When I click "Account" on the navigation bar, it just takes me back to index. I can see why this is happening - clicking Account is a GET request. But if I take out methods specification and just do:
@app.route("/account")
@login_required
def account():
# update pw
return redirect(url_for("index"))
I get the following error on my browser:
Bad Request
The browser (or proxy) sent a request that this server could not understand.
I read that if you don't specify GET or POST, it's GET by default. So it makes sense that index(), history(), logout() don't specify methods as they only get back results, not send something to be changed.
I think this is happening because I'm trying to change the database (which should be through a POST request) but I'm sending a GET request? I'm lost on how to implement this route.