Submitting data to Flask with HTML Forms
Contents
Disclaimer: This page is entirely static and does not interact with any server, all client-server interactions are purely simulated
HTML Forms
The source code of this HTML form looks like:
|
|
Try It!
Write your name and age into the above form.
When you press the Submit button, have a look at the URL!
Forms using GET
Have a look at the address bar - We can see the data that we just submitted.
This type of form submission uses a GET
request
Great, or is it…
Try It!
Modify the ?name=&age=
portion of the URL and change the name!
Submitting form data via a GET request is somewhat insecure, as it allows the client to easily tamper with the data. (It also makes the URL look yucky!)
Instead, what we can do is change our form to use the POST
request method!!!
Forms using POST
The HTML code follows the standard form convention, but with a method="POST"
attribute in the <form>
tag:
|
|
Try It!
Write your name and age into the above form.
Then press the Submit button!
This time, there’s no data in the URL - rather the data was sent to the server via a POST
request!
Forms and Flask
Handling GET
To retrieve the query arguments in Flask
1) Import the request proxyfrom flask import request
2) Access the datarequest.args
Example
|
|
Handling POST
To retrieve the post data in Flask
1) Import the request proxyfrom flask import request
2) Access the datarequest.data
(Make sure your route decorator has POST
in its method scope!)
Example
|
|
Use Cases
Keeping submitted data
If you noticed in the above forms…
When you pressed the Submit button, after the page loaded with the response - your form was empty!
This is because HTTP is a stateless protocol, each request doesn’t know about any other request.
As a result, the response webpage will not be able to know the submitted data… unless we make it so
This can easily be done with Flask’s render_template
function, where we can pass the submitted form data back into the webpage!
Refer to this file from the week 7 / 8 lab…
Example
|
|
|
|
Demo
Client-side validation
It is good practice to also validate form data on the client-side as well as on the server-side!
(Note: Server-side validation is a MUST, regardless if you have client-side validation or not)
To do such, you can add the required
attribute to your <input>
tags.
For example: <input type="number" name="age" required>
This prevents the browser from submitting the form unless the input is filled, and has valid content corresponding to the input type
(In fact, all the forms on this page already have this attribute added!)