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




# passing required methods
@app.route('/name', methods=['GET', 'POST'])
def name():
    if request.method == 'POST':
        # if form is submitted this code will be executed
        name = request.form.get('name')
        email = request.form.get('email')
        return render_template("name.html", name=name, email=email)
    else:
        # if only visited
    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. 

How to Obtain Values of Request Variables in Flask

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




# importing Flask and request
# from the flask library
from flask import Flask, request, render_template  # to render html code
  
# pass current module (__name__)
# as argument to create instance of flask app
app = Flask(__name__)
  
# ‘/search’ URL is bound with search() function.
  
  
@app.route('/search'# for requesting query arguments
# defining a function search which returns the rendered html code
def search():
    return render_template("search.html")
  
# ‘/name’ URL is bound with name() function.
  
  
@app.route('/name'# for requesting form data
# defining a function name which returns the rendered html code
def name():
    return render_template("name.html")
  
  
# for running the app
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.

Similar Reads

Requesting query arguments

...

Requesting form data

As explained above a query string is the part after the question mark:...

Summary

...

Contact Us