The purpose of this exercise is to teach you how to make the Browser take user input, transmit it, and make the server receive and process it. Then, in exercises. 4.2, 4.3, and 4.4, you will add features. You will do this on the Contact page in the Flask project from Exercise 2.2, starting by adding a form.
Click here for demo of what you will make in this exercise. You may want to download the CSS from the Demo to replace static/index.css unless you prefer to use your own.
In this step, add an HTML form as a second contact option in contact.html (you may also add some text as shown in the Demo) and create a confirmation page in the templates directory.
Before you can receive and process data, add an import: from flask import request (or add it to your import list). When imported, "request" is an object that gives you access to HTTP header information, including data submitted through forms. We will learn to use some of its attributes -- request.args, request.form, and request.files -- but it also has many more, which you can look up in Flask's documentation.
Add the following route to app.py and create the following form in the templates directory.
Prove to yourself that you can achieve the same thing as submitting a form by using URLs with a ? followed by key-value pairs. For example (replacing ... with your URL for this exercise):
.../contact.html?name=Helga&location=UVU&need=Someone%20stole%20my%20sleep
.../contact.html?name=Ralph&location=CS%20520&need=I%20am%20being%20held%20captive%20by%20Homework
.../contact.html?name=Diana&location=Food%20Court&need=I%20am%20really%20hungry.
Add a new route to handle POST data instead of GET data. Compare the very small differences between this route and the alert_get route above (that it uses method=["POST"] and that it uses request.form instead of request.args). Change the <form> line to use POST and point at the new route:
After doing this, use the Developer Tools => Network tab as described above in the "Get Comfortable with Forms" section above and watch how the Payload subtab tracks the POST data being sent.
The next exercise uses it.