0
0
Flaskframework~30 mins

Trailing slash behavior in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Trailing Slash Behavior in Flask Routes
📖 Scenario: You are building a simple Flask web app that has two pages: a homepage and an about page. You want to understand how Flask handles URLs with and without trailing slashes.
🎯 Goal: Create two Flask routes: one for the homepage at / and one for the about page at /about/. Learn how Flask redirects URLs when the trailing slash is missing or present.
📋 What You'll Learn
Create a Flask app instance named app
Define a route for the homepage at / that returns the text 'Welcome to the homepage!'
Define a route for the about page at /about/ that returns the text 'About us page'
Observe how Flask handles requests to /about without the trailing slash
💡 Why This Matters
🌍 Real World
Web developers often need to control URL formats for SEO and user experience. Understanding trailing slash behavior helps avoid broken links and unexpected redirects.
💼 Career
Knowing how Flask handles trailing slashes is important for backend developers building web applications to ensure clean and consistent URLs.
Progress0 / 4 steps
1
Create the Flask app and homepage route
Import Flask from flask and create a Flask app instance called app. Then define a route for the homepage at / that returns the string 'Welcome to the homepage!'.
Flask
Need a hint?

Start by importing Flask and creating the app instance. Then use @app.route('/') to define the homepage route.

2
Add a trailing slash route for the about page
Add a new route to app for the URL /about/ (with a trailing slash). Define a function called about that returns the string 'About us page'.
Flask
Need a hint?

Use @app.route('/about/') to define the about page route with a trailing slash.

3
Test Flask's trailing slash redirect behavior
Explain what happens if a user visits /about (without the trailing slash) in their browser. Add a comment in the code describing Flask's automatic redirect behavior for routes defined with a trailing slash.
Flask
Need a hint?

Write a comment inside the about function explaining Flask's redirect from /about to /about/.

4
Add a route without trailing slash and observe behavior
Add a new route for /contact (without trailing slash) that returns 'Contact page'. Add a comment explaining that Flask will NOT redirect if the trailing slash is missing in the route definition.
Flask
Need a hint?

Define the /contact route without a trailing slash and add a comment about Flask's behavior.