Trailing slash behavior helps Flask decide if a URL should end with a slash or not. It makes your web app handle URLs more flexibly and avoid errors.
Trailing slash behavior in Flask
@app.route('/path/') # redirects /path to /path/ @app.route('/path') # 404 on /path/
Adding a trailing slash in the route means Flask will redirect URLs without the slash to the one with it.
Not adding a trailing slash means Flask will return 404 if the slash is added in the URL.
@app.route('/hello/') def hello(): return 'Hello with slash!' # Accessing '/hello' redirects to '/hello/'
@app.route('/world') def world(): return 'Hello without slash!' # Accessing '/world/' returns 404 error
This Flask app has two routes: one with a trailing slash and one without. Visiting '/greet' will redirect to '/greet/'. Visiting '/bye/' will cause a 404 error because the route does not have a trailing slash.
from flask import Flask app = Flask(__name__) @app.route('/greet/') def greet_slash(): return 'Greet with slash!' @app.route('/bye') def bye_no_slash(): return 'Bye without slash!' if __name__ == '__main__': app.run(debug=True)
Flask's trailing slash behavior helps keep URLs consistent and user-friendly.
Use trailing slashes for routes that represent folders or collections.
Use no trailing slash for routes that represent single items or actions.
Trailing slash in Flask routes controls URL redirection and 404 errors.
Routes with trailing slash redirect URLs without it to the slash version.
Routes without trailing slash do not accept URLs with an extra slash.
Practice
@app.route('/home/') and a user visits /home without the slash?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 CQuick Check:
Trailing slash route redirects missing slash URL [OK]
- Thinking Flask serves both URLs without redirect
- Assuming 404 error occurs without slash
- Believing Flask throws server error
Solution
Step 1: Identify correct route syntax without trailing slash
To define a route that does not accept a trailing slash, the route string should not end with a slash.Step 2: Check each option
@app.route('/about') uses '/about' without a trailing slash, which is correct syntax.Final Answer:
@app.route('/about') -> Option DQuick Check:
Route without trailing slash = no slash in route string [OK]
- Adding extra slashes in route string
- Omitting leading slash in route
- Confusing trailing slash with double slashes
@app.route('/page/')
def page():
return 'Page with slash'What will happen if a user visits
/page in their browser?Solution
Step 1: Analyze route definition with trailing slash
The route '/page/' expects URLs ending with a slash.Step 2: Understand Flask's redirect behavior
If the user visits '/page' without the slash, Flask redirects to '/page/' automatically and serves the response.Final Answer:
The user is redirected to /page/ and sees 'Page with slash'. -> Option BQuick Check:
Trailing slash route redirects missing slash URL [OK]
- Assuming no redirect happens
- Expecting 404 error on missing slash
- Thinking server crashes on missing slash
@app.route('/contact')
def contact():
return 'Contact page'What happens if a user visits
/contact/ with a trailing slash?Solution
Step 1: Identify route without trailing slash
The route '/contact' is defined without a trailing slash.Step 2: Check Flask behavior on extra trailing slash
Flask does NOT redirect URLs with an extra trailing slash to the no-slash route; it returns a 404 error.Final Answer:
Flask returns a 404 Not Found error. -> Option AQuick Check:
No trailing slash route rejects extra slash URL [OK]
- Thinking Flask redirects to no-slash URL
- Assuming page loads normally with slash
- Believing server throws error instead of 404
/blog and /blog/ URLs without redirecting or 404 errors. Which approach achieves this?Solution
Step 1: Understand default Flask behavior
By default, Flask treats routes with and without trailing slash differently, redirecting or 404ing as needed.Step 2: Use strict_slashes=False to accept both
Settingstrict_slashes=Falseon the route decorator disables strict trailing slash matching, allowing both URLs without redirect or error.Final Answer:
Use Flask's strict_slashes=False option on the route decorator. -> Option AQuick Check:
strict_slashes=False allows both URLs [OK]
- Defining two routes duplicates code
- Relying on default redirect behavior
- Ignoring strict_slashes option
Ready to prove it?
Pass the test and this pattern moves unknown
