Retrieving HTML Form data using Flask
Last Updated :
30 May, 2022
Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.
Read this article to know more about Flask Create form as HTMLÂ
We will create a simple HTML Form, very simple Login form
html
<form action="{{ url_for("gfg")}}" method="post">
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="fname" placeholder="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lname" placeholder="lastname">
<button type="submit">Login</button>
Its an simple HTML form using the post method the only thing is unique is action URL. URL_for is an Flask way of creating dynamic URLs where the first arguments refers to the function of that specific route in flask. In our form it will create a Dynamic route which has gfg function in the flask app
Create Flask application
Start your virtual environment
pip install virtualenv
python3 -m venv env
pip install flask
Now we will create the flask backend which will get user input from HTML form
Python3
# importing Flask and other modules
from flask import Flask, request, render_template
# Flask constructor
app = Flask(__name__)
# A decorator used to tell the application
# which URL is associated function
@app.route('/', methods =["GET", "POST"])
def gfg():
if request.method == "POST":
# getting input with name = fname in HTML form
first_name = request.form.get("fname")
# getting input with name = lname in HTML form
last_name = request.form.get("lname")
return "Your name is "+first_name + last_name
return render_template("form.html")
if __name__=='__main__':
app.run()
Working -Â
Almost everything is simple, We have created a simple Flask app, if we look into code
- importing flask and creating a home route which has both get and post methods
- defining a function with name gfg
- if requesting method is post, which is the method we specified in the form we get the input data from HTML form
- you can get HTML input from Form using name attribute and request.form.get() function by passing the name of that input as argument
- request.form.get("fname") will get input from Input value which has name attribute as fname and stores in first_name variable
- request.form.get("lname") will get input from Input value which has name attribute as lname and stores in last_name variable
- The return value of POST method is by replacing the variables with their valuesYour name is "+first_name+last_name
- the default return value for the function gfg id returning home.html template
- you can review what-does-the-if-__name__-__main__-dofrom the article
Output - Code in action
flask server running
html form
returning data from html template 
Similar Reads
Python | Using for loop in Flask Prerequisite: HTML Basics, Python Basics, Flask It is not possible to write front-end course every time user make changes in his/her profile. We use a template and it generates code according to the content. Flask is one of the web development frameworks written in Python. Through flask, a loop can
3 min read
How to get data from 'ImmutableMultiDict' in flask In this article, we will see how to get data from ImmutableMultiDict in the flask. It is a type of Dictionary in which a single key can have different values. It is used because some elements have multiple values for the same key and it saves the multiple values of a key in form of a list. It is usu
2 min read
How CGI Scripts Receive data from HTML forms? In this article, we will see how to revive data from HTML forms in CGI Scripts (Common Gateway Interface). We will perform the demonstration on a Windows Operating System machine. We will create a basic HTML form and a CGI script using Python, which will handle the data received from the HTML form.R
5 min read
Using Request Args for a Variable URL in Flask This article will teach us how to use Request Arguments in our Flask application. First, we will understand. What are the Request Arguments? What are the Request Arguments? Request Arguments are MultiDict objects with the parsed contents of the query string (the part in the URL after the question ma
4 min read
Single Page Portfolio using Flask In this article, weâll discuss how to create a single-page portfolio webpage using the Flask framework. This project demonstrates how to build an impressive portfolio to showcase your skills and experience to HR professionals, colleagues, or potential employers.Key features of this portfolio are-Dow
7 min read