In this tutorial, we will be learning about creating routes and handling HTTP requests in Flask, a Python micro web framework. Flask allows us to map URLs to Python functions, which are also called "routes". This is a fundamental aspect of building any web application, as it allows us to determine what content is displayed to the user based on the URL they are visiting.
By the end of this tutorial, you will understand what routes are, how to create them, and how to handle HTTP requests.
Prerequisites: This tutorial assumes that you have a basic understanding of Python and a basic knowledge of HTML. Familiarity with Flask would be beneficial but isn't necessary.
A route is a URL pattern that is used to fetch a particular webpage. When a URL matches a route, the associated Python function is executed, and its result is returned to the browser as a response.
HTTP requests are how we communicate with a server. The four most common types of requests are GET, POST, PUT, and DELETE.
Creating a route in Flask is straightforward. We use the route()
decorator to bind a function to a URL. Here is an example:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
In this example, '/'
is the URL to which we are binding the function home()
. When we navigate to the root URL of our application, we will see the text "Hello, World!".
With Flask, we can easily handle different types of HTTP requests. By default, a route only answers to GET requests. But you can use the methods
argument of the route()
decorator to handle other types of requests. For example:
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return do_the_login()
else:
return show_the_login_form()
In this example, the login()
function will respond to both GET and POST requests. If the request method was POST, then the login form is posted. If the request method was GET, then the login form is displayed.
from flask import Flask
app = Flask(__name__)
# This is a route
@app.route('/')
def home():
# This function will be called when the '/' URL is accessed
return "Hello, World!"
When you navigate to the root URL of your application, you will see "Hello, World!".
@app.route('/hello/<name>')
def hello(name):
# The <name> inside the route is a variable
return "Hello, %s!" % name
In this case, if you navigate to '/hello/John', you will see "Hello, John!".
from flask import request
@app.route('/login', methods=['GET', 'POST'])
def login():
# Here we check the method of the request
if request.method == 'POST':
return do_the_login()
else:
return show_the_login_form()
In this case, the login()
function will respond to both GET and POST requests.
In this tutorial, we have learned about creating routes and handling HTTP requests in Flask. We learned how to bind Python functions to URLs and how to handle different types of HTTP requests.
Next steps in your learning could include learning about templates in Flask, form handling, and connecting to databases. Here are some additional resources:
- Flask Documentation
- The Flask Mega-Tutorial
The solutions can be found in the Flask documentation. Keep practicing to solidify your understanding!