Requests#
Author: Mike Wood
Learning Objectives: By the end of this notebook, you should be able to:
Implement a GET request to retrieve public data
Implement a POST request to implement a hidden request
GET Requests#
In this first example, let’s consider a typical example of a user enters some information - say, their name into a registration form, as shown on the page HERE - and then this information is reflected in a subsequent page (i.e. it is public).
# import the Flask class from the flask module
from flask import Flask
# import the render_template function
from flask import render_template
# import the request function
from flask import request
# create a Flask object called app
app = Flask(__name__)
# define a route to the home page
# create a fillable_get_form function
@app.route("/")
@app.route("/home")
def fillable_get_form():
return render_template('simple_form.html')
# define a route to the person page
# add 'GET' to the methods
# create a person function
@app.route("/person", methods=['GET'])
def person():
# retrieve the fname and lname from the requests
fname = request.args['fname']
lname = request.args['lname']
# pass the fname and lname args to the render template
# for the person_form.html script
return render_template('person_form.html', fname=fname, lname=lname)
This page generates a submittable form which is created with the simple_form.html template:
<!doctype html>
<html>
<head>
<title>Submit Form</title>
<meta name="description" content="A page to demo submitting a form">
</head>
<body>
<!--Add a header for the registration form-->
<h2>Registration Form</h2>
<!--Make a form with person action and get method-->
<form action="/person" method="get">
<!--Add a a label and input for first name
Be sure to link the name to the fname variable-->
<label>First name:</label><br>
<input type="text" name="fname"><br>
<!--Add a a label and input for last name
Be sure to link the name to the lname variable-->
<label>Last name:</label><br>
<input type="text" name="lname"><br><br>
<!--Make a submit button with the text Register-->
<input type="submit" value="Register">
</form>
</body>
</html>
As you can see, the input fields are provided with keys for name=fname and name=lname in these lines:
<input type="text" name="fname"><br>
<input type="text" name="lname"><br>
Note that these are passed explicitly to the person_page.html template in the following lines:
fname = request.args['fname']
lname = request.args['lname']
return render_template('person_form.html', fname=fname, lname=lname)
In the person_page.html template, the values in fname and lname are then implemented as variables {{fname}} and {{lname}} as shown below:
<!doctype html>
<html>
<head>
<title>Person Form</title>
<meta name="description" content="A page to describe a person">
</head>
<body>
<h2>Person Details</h2>
<form>
<!--Add a labels for the first name and lat name-->
<label>First name: {{fname}}</label><br>
<label>Last name: {{lname}}</label><br>
</form>
</body>
</html>
You can try it for yourself on your machine. When entering a name on your page, you should get a page similar to the one shown HERE.
Note that the GET requests displays the requested information directly in the URL. In effect, this type of information should not be used for sensitive data.
POST Requests#
In contrast to GET requests, POST request are hidden and are more suitable for a form where information will be submitted.
To implement a POST request, we can modify the person function above as follows:
@app.route("/person", methods=['POST'])
def person():
# pass the form to the person_form page
form = request.form
# using the request.form attribute
return render_template('person_form.html', form=form)
As we can see, the lname and fname variables are no longer passed explicitly. Instead a form is passed.
This is reflected in the registration page by updating the request method to post as follows:
<form action="/person" method="post">
Then, in the person form, the variables are now accessed as attributes of the form that has been passed to the template:
<label> First Name: {{form.fname}}</label><br>
<label> Last Name: {{form.lname}}</label><br>
Now, when looking at the person_form.html page, the registration image will no longer be presented in the header of the page.