Defining Routes and URL Building in Flask
Course Title: Mastering Flask Framework: Building Modern Web Applications Section Title: Routing, Views, and Templates Topic: Defining routes and URL building in Flask
Introduction to Routing in Flask
Routing is a crucial aspect of building web applications. It involves defining URLs and mapping them to specific functions or views in your application. In Flask, routing is handled using the @app.route()
decorator. This decorator takes in a URL path as a string and maps it to a specific function in your application.
Defining Routes in Flask
To define a route in Flask, you use the @app.route()
decorator followed by the URL path. Here's an example of defining a simple route:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome to the homepage!'
if __name__ == '__main__':
app.run(debug=True)
In this example, the @app.route('/')
decorator defines a route for the root URL of your application (/
). When a user accesses this URL, the home()
function is called, which returns a simple string.
Route Variables
Flask allows you to pass variables in your route paths using angle brackets (<>
). Here's an example of defining a route with a variable:
@app.route('/hello/<name>')
def hello(name):
return f'Hello, {name}!'
In this example, the @app.route('/hello/<name>')
decorator defines a route that accepts a variable name
in the URL path. The name
variable is then passed to the hello()
function, which returns a string with the variable inserted.
Route Variable Types
Flask supports several types of route variables, including:
string
: The default type, which accepts any string.int
: Accepts integers only.float
: Accepts floating-point numbers only.path
: Accepts any path string.any
: Accepts any of the above types.
Here's an example of defining a route with an integer variable:
@app.route('/user/<int:user_id>')
def user(user_id):
return f'Welcome, user {user_id}!'
URL Building with url_for()
Flask provides a function called url_for()
to build URLs based on your route definitions. This function takes in the function name of your route as a string and returns the corresponding URL. Here's an example of using url_for()
:
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome to the homepage!'
@app.route('/hello/<name>')
def hello(name):
return f'Hello, {name}!'
@app.route('/dynamic')
def dynamic():
url = url_for('hello', name='John')
return f'The URL for the hello route is: {url}'
if __name__ == '__main__':
app.run(debug=True)
In this example, the url_for()
function is used in the dynamic()
function to generate the URL for the hello()
route with a variable name
set to 'John'
.
Key Concepts and Takeaways
- Use the
@app.route()
decorator to define routes in Flask. - Use angle brackets (
<>
) to pass variables in your route paths. - Use route variable types to restrict input types.
- Use
url_for()
to build URLs based on your route definitions.
Additional Resources
What's Next?
In the next topic, we will cover creating views and rendering templates with Jinja2. You can move on to that topic once you feel comfortable with defining routes and building URLs in Flask.
Leave a comment below if you have any questions or need help with the concepts covered in this topic.
Images

Comments