Now that we've created our form, the next step is to get it to work. We need our Flask server to be able to receive the data entered by the user.
At the moment when you fill out the form and press "Ok", nothing happens.
In order for it to send the form data, we have to change our HTML form. It needs to have an action
and method
.
1. CHALLENGE: See if you can use the documentation below to figure out how to make our HTML form submit a "POST"
request to the path "/login"
.
https://www.w3schools.com/tags/att_form_method.asp
https://www.w3schools.com/tags/att_form_action.asp
SOLUTION: https://gist.github.com/angelabauer/889ac7cfdfed5cfeb79559f41c9c6a07
2. Once the form is submitted, we also need to catch this POST request in our server. To do this we first need to give each input in our form a name
attribute.
3. Now we can create a decorator in our main.py that will trigger a method when it receives a POST request:
See what happens when you return
a string.
Notice that the methods
parameter accepts a list, so you can have multiple methods targeted by one route. e.g.
@app.route("/contact", methods=["GET", "POST"]
More on this in the documentation here: https://flask.palletsprojects.com/en/2.3.x/quickstart/#http-methods
SOLUTION: https://gist.github.com/TheMuellenator/98b340b08ea8ed51b7f04ccf254a86e7
4. Flask has a method called request
(don't confuse this with the requests module) which allows us to tap into the parameters of the request that was made to our server.
DIFFICULT CHALLENGE: See if you can use the Flask documentation below to figure out how to get hold of the name and password that was entered into the form and send it back to the client as a <h1>. e.g.:
Documentation:
https://flask.palletsprojects.com/en/2.3.x/quickstart/#the-request-object
HINT:
https://stackoverflow.com/questions/11556958/sending-data-from-html-form-to-a-python-script-in-flask
SOLUTION in the next lesson.