Search⌘ K

Data Handling Using the Request Object

Explore how to use Flask's global request object to determine request methods and extract form data for applications. Learn to implement user validation with form input, handling both successful and failed login attempts by processing POST requests and rendering messages in templates.

📌 How can we know if a particular request is GET or POST?
This is where the request object comes in handy. Let’s find out more about the request object.

Accessing form data

To access the data sent by a user, we use the global request object. Before we start using it, let’s import it from the flask module.

from flask import request

Getting the Method type from request

We can use the method member variable of the request object to determine the method of an incoming request.
Consider the snippet given below.

Python 3.5
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
...
else
...
return render_template("login.html")

Explanation #

...