📖 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
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
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
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
Hint
Define the /contact route without a trailing slash and add a comment about Flask's behavior.
Practice
(1/5)
1. In Flask, what happens if you define a route with a trailing slash like @app.route('/home/') and a user visits /home without the slash?
easy
A. Flask returns a 404 Not Found error.
B. Flask serves the /home route without redirecting.
C. Flask redirects the user to /home/ automatically.
D. Flask throws a server error.
Solution
Step 1: Understand trailing slash behavior in Flask routes
Routes defined with a trailing slash expect URLs to end with a slash.
Step 2: Check Flask's automatic redirect behavior
If a user visits the URL without the trailing slash, Flask automatically redirects to the URL with the slash.
Final Answer:
Flask redirects the user to /home/ automatically. -> Option C