Routes & URLs – Flask Tutorial Series #2
When building a web application using Flask, routes and URLs play a crucial role in defining the logic of your application. In this tutorial, we will explore how routes and URLs work in Flask.
What is a Route?
In Flask, a route is a mapping between a URL path and a Python function that handles requests to that path. When a user makes a request to a specific URL, Flask uses the routes defined in your application to determine which Python function should be called to handle the request.
Defining Routes in Flask
To define a route in Flask, you use the @app.route
decorator before a Python function. The decorator takes the URL path as an argument. For example, to create a route for the home page of your application, you would use the following code:
@app.route('/') def home(): return 'Welcome to the home page'
In this example, the /
URL path corresponds to the home function, which returns the message ‘Welcome to the home page’ when accessed.
Dynamic Routes
Flask also supports dynamic routes, which allow you to capture variables from the URL path and pass them to your Python function. To define a dynamic route, you use angle brackets <>
in the URL path to specify the variable. For example, to create a route that accepts a user’s name as a parameter, you would use the following code:
@app.route('/hello/<name>') def hello(name): return f'Hello, {name}'
In this example, the /hello/<name>
URL path captures the user’s name and passes it as a parameter to the hello function, which then greets the user by name.
URLs in Templates
When generating URLs in Flask templates, you can use the url_for
function to generate URLs based on the route name. For example, if you have a route named ‘home’, you can generate the URL for that route in a template using the following code:
<a href="{{ url_for('home') }}">Home</a>
This will generate the URL for the ‘home’ route, allowing users to navigate to that route by clicking the link.
Conclusion
Routes and URLs are essential components of any Flask application, as they define how users interact with your web application. By properly defining routes and URLs, you can create a well-structured and navigable web application that provides users with a seamless experience.
still going awesome… me .. still learning…. great ! thx
On 9:25 the error is generated by trying to access the variable 'greeting' directly using the brackets []. If use it get like on the variable below 'name', the endpoint will still work even if nothing is provided on the URL
Great video, keep going!
Great work, Florian!
Thanks for the videos! I started learning python and flask around two or three months ago. Almost done with my first site but I like seeing your tutorials on it because I want to make sure I haven't missed anything or misunderstood anything. Even though I know how to do some things I don't feel like I grasp the rational behind them or why they work the way they do. Hoping to learn a lot from you.
Loving these videos!
Great video
more pls
Thanks for this content! I wanna ask "I'm trying to build a website like youtube using flask but i didn't understand how to store video in database or where i should store them when i make the website online?"
Thanks!
You're videos are amazing 😮
❤
The first one here