0
0
Flaskframework~20 mins

Trailing slash behavior in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trailing Slash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when accessing a Flask route with a trailing slash?
Consider the following Flask route:
 @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!'
AFlask will serve the 'Hello!' response without redirect.
BFlask will return a 404 Not Found error.
CFlask will redirect the user to '/hello/' automatically.
DFlask will raise a runtime error due to missing slash.
Attempts:
2 left
💡 Hint
Think about how Flask handles routes ending with a slash versus those without.
component_behavior
intermediate
2:00remaining
What is the behavior of a Flask route without a trailing slash when accessed with one?
Given this Flask route:
 @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'
AFlask will return a 404 Not Found error.
BFlask will redirect the user to '/about' (without slash).
CFlask will raise a runtime error due to extra slash.
DFlask will serve the 'About page' response without redirect.
Attempts:
2 left
💡 Hint
Consider how Flask treats routes defined without trailing slashes.
📝 Syntax
advanced
2:00remaining
Which Flask route definition causes a syntax error?
Identify the option that will cause a syntax error when defining a Flask route:
A
@app.route('/profile/'
def profile():
    return 'Profile'
B
@app.route('/contact')
def contact():
    return 'Contact'
C
@app.route('/home')
def home():
    return 'Home'
D
@app.route('/dashboard/')
def dashboard():
    return 'Dashboard'
Attempts:
2 left
💡 Hint
Check for missing or mismatched parentheses in the route decorator.
state_output
advanced
2:00remaining
What is the output when accessing a Flask route with and without trailing slash?
Given this Flask app code:
 @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'
AVisiting '/item' returns 'Item without slash'; visiting '/item/' returns 'Item with slash'.
BBoth URLs return 'Item without slash'.
CBoth URLs return 'Item with slash'.
DVisiting '/item' redirects to '/item/' and returns 'Item with slash'.
Attempts:
2 left
💡 Hint
Routes are defined separately for each URL pattern.
🔧 Debug
expert
3:00remaining
Why does this Flask app cause a redirect loop?
Examine the Flask app code:
 @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/')
AFlask automatically resolves the loop and serves the page.
BThe app raises a runtime error due to conflicting routes.
COnly '/loop/' causes a redirect loop; '/loop' works fine.
DBoth routes redirect to each other endlessly causing a loop.
Attempts:
2 left
💡 Hint
Check what each route redirects to and if it matches the other route.