How to Obtain Values of Request Variables in Flask
Last Updated :
23 Jan, 2023
In this article, we will see how to request the query arguments of the URL and how to request the incoming form data from the user into the flask.
In a flask, to deal with query strings or form data, we use Request variables.
For example, a query string may look like this:
google.com/search?q=python
Let’s start by creating a demo flask project. To be able to request data we will need to import the request from the Flask library.
Python3
from flask import Flask, request, render_template
app = Flask(__name__)
@app .route( '/search' )
def search():
return render_template( "search.html" )
@app .route( '/name' )
def name():
return render_template( "name.html" )
if __name__ = = "__main__" :
app.run(debug = True )
|
The route() function of the Flask class is a decorator, which tells the application which URL should call the associated function.
Here we have created two routes /search to request query arguments and /name to request form data.
We will update them as we move ahead. Again details can be understood from here.
Requesting query arguments
As explained above a query string is the part after the question mark:
https://www.google.com/search?q=python
Here in our case the app, by default, runs on 127.0.0.1:5000 so suppose we add a query string to /search route for performing a search, it will look like this with key query and value g4g :
http://127.0.0.1:5000/search?query=g4g
Now we want to extract the value. For that we use request.args.get(‘query’) or request.args[‘query’] .
It is recommended to use request.args.get(‘query’) as the second will run into an 400 error if the query key is not provided.
So let’s make changes to our /search route
Python3
@app .route( '/search' )
def search():
query = request.args.get( 'query' )
return render_template( "search.html" , query = query)
|
This will render this HTML code:
HTML
<!DOCTYPE html>
< html >
< head >
< title >Search</ title >
</ head >
< body >
< h2 >You searched for {{query}}</ h2 >
</ body >
</ html >
|
So after running the app and going to
http://127.0.0.1:5000/search?query=g4g
will give this output:
Requesting form data
Now if we are required to extract data from the POST method. Let’s take a simple example by requesting the name and email of a user in a form:
Code for the above form:
HTML
<!DOCTYPE html>
< html >
< head >
< title >Name</ title >
</ head >
< body >
< form action = "/name" method = "POST" >
< label >Enter your name:</ label >
< input type = "text" name = "name" required />
< br >
< label >Enter your email:</ label >
< input type = "text" name = "email" required />
< input type = "submit" value = "Submit" >
</ form >
< br >< br >
DATA RECEIVED:
< br >
{{name}}
< br >
{{email}}
</ body >
</ html >
|
So to request the name and email from the form we use request.form.get(‘name’) and request.form.get(’email’) as the name of HTML has value name and email in the input tag:
<input type="text" name="name" required />
<input type="text" name="email" required />
Now let’s see the Python code:
Here we require to give the methods we are using in the route. So when the form is submitted, the code from the POST method is executed, and if the method is GET the code from the else condition is executed.
Python3
@app .route( '/name' , methods = [ 'GET' , 'POST' ])
def name():
if request.method = = 'POST' :
name = request.form.get( 'name' )
email = request.form.get( 'email' )
return render_template( "name.html" , name = name, email = email)
else :
return render_template( "name.html" )
|
So now if the URL is visited and the data is entered on:
http://127.0.0.1:5000/name
This page looks like this:
and after submitting the form, the if condition is evaluated as true and the values are requested and returned back to the HTML code. The page now looks like this:
Instead of using request.form.get(’email’) we can also use request.form[’email’] and results will be the same, the difference is the second one is returned in the form of a dictionary.
Summary
To request data of a query string we use:
request.args.get('query') //recommended or request.args['query']
To request data of a form we use:
request.form.get('name') //recommended or request.form['name']
Share your thoughts in the comments
Please Login to comment...