Challenge - 5 Problems
Trailing Slash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when accessing a Flask route with a trailing slash?
Consider the following Flask route:
What will happen if a user visits
@app.route('/hello/')
def hello():
return 'Hello!'What will happen if a user visits
/hello (without the trailing slash) in their browser?Flask
@app.route('/hello/') def hello(): return 'Hello!'
Attempts:
2 left
💡 Hint
Think about how Flask handles routes ending with a slash versus those without.
✗ Incorrect
When a route is defined with a trailing slash, Flask treats it as a directory. Accessing the same route without the slash causes Flask to redirect to the slash version automatically.
❓ component_behavior
intermediate2:00remaining
What is the behavior of a Flask route without a trailing slash when accessed with one?
Given this Flask route:
What happens if a user visits
@app.route('/about')
def about():
return 'About page'What happens if a user visits
/about/ (with a trailing slash)?Flask
@app.route('/about') def about(): return 'About page'
Attempts:
2 left
💡 Hint
Consider how Flask treats routes defined without trailing slashes.
✗ Incorrect
When a route is defined without a trailing slash, Flask expects the URL exactly as defined. Accessing it with a trailing slash causes Flask to redirect to the no-slash version automatically.
📝 Syntax
advanced2:00remaining
Which Flask route definition causes a syntax error?
Identify the option that will cause a syntax error when defining a Flask route:
Attempts:
2 left
💡 Hint
Check for missing or mismatched parentheses in the route decorator.
✗ Incorrect
Option A is missing a closing parenthesis in the @app.route decorator, causing a syntax error.
❓ state_output
advanced2:00remaining
What is the output when accessing a Flask route with and without trailing slash?
Given this Flask app code:
What will be the output when visiting
@app.route('/item')
def item():
return 'Item without slash'
@app.route('/item/')
def item_slash():
return 'Item with slash'What will be the output when visiting
/item and /item/ respectively?Flask
@app.route('/item') def item(): return 'Item without slash' @app.route('/item/') def item_slash(): return 'Item with slash'
Attempts:
2 left
💡 Hint
Routes are defined separately for each URL pattern.
✗ Incorrect
Each route is defined explicitly. Flask matches the exact route, so '/item' and '/item/' call different functions and return different responses.
🔧 Debug
expert3:00remaining
Why does this Flask app cause a redirect loop?
Examine the Flask app code:
What is the cause of the redirect loop when visiting '/loop/' or '/loop'?
@app.route('/loop/')
def loop():
return redirect('/loop')
@app.route('/loop')
def loop_no_slash():
return redirect('/loop/')What is the cause of the redirect loop when visiting '/loop/' or '/loop'?
Flask
@app.route('/loop/') def loop(): return redirect('/loop') @app.route('/loop') def loop_no_slash(): return redirect('/loop/')
Attempts:
2 left
💡 Hint
Check what each route redirects to and if it matches the other route.
✗ Incorrect
The '/loop/' route redirects to '/loop', and '/loop' redirects back to '/loop/'. This causes an infinite redirect loop between the two URLs.