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
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
Flask SQLAlchemy Tutorial for Database Flask doesnât have a built-in way to handle databases, so it relies on SQLAlchemy, a powerful library that makes working with databases easier. SQLAlchemy provides an Object Relational Mapper (ORM), allowing developers to interact with databases using Python code instead of raw SQL. This brings seve
7 min read
Python | Build a REST API using Flask Prerequisite: Introduction to Rest API REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data. In this article, we will build a REST API in Python using the Fla
3 min read
How to Get Data from API in Python Flask In modern web development, APIs (Application Programming Interfaces) play a crucial role in enabling the interaction between different software systems. Flask, a lightweight WSGI web application framework in Python, provides a simple and flexible way to create APIs. In this article, we'll explore ho
2 min read
Retrieve Text From Textarea in Flask In this article, we will see how we can retrieve the elements inside the textarea tag using Python. Textarea is one of the popular tags we used in HTML. To retrieve text from a textarea in Flask, you can use the request.form.get() method. This method allows you to retrieve the value of a specific fo
2 min read
Todo list app using Flask | Python There are many frameworks that allow building your webpage using Python, like Django, flask, etc. Flask is a web application framework written in Python. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine. Its modules and libraries that help the developer to writ
3 min read