Take User Input (HTML Forms and URLs)

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.

Add the Form to contact.html

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.

Add to templates/contact.html
Add the following HTML to make a form. The demo also adds additional text, which you are free to use or not to use.

Receive and Process Form Data on Back End

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.

app.py
templates/contact_confirm.html

Add Basic Back-End Validation

Add code to back end alert_get route to make sure that the required fields name, location, and need have values, and add code to contact.html that displays a message when the message variable is set. The demo above uses the following at the to display a message at the top of the form, but feel free to rewrite it if desired:
Add to app.py
Add somewhere in contact.html

Get Comfortable with Forms, Sending and Receiving

Do the following in order to get comfortable with HTML forms, sending and receiving:

Use URL Instead of Form to Transmit GET data

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.

Change to POST method

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:

app.py
Change line in contact.html

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.

Required Reading

Read this article on the differences between GET and POST, when to use each and why.

Don't Delete Your Code Yet

The next exercise uses it.