Bird
Raised Fist0
Flaskframework~20 mins

Trailing slash behavior in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. Step 1: Understand trailing slash behavior in Flask routes

    Routes defined with a trailing slash expect URLs to end with a slash.
  2. 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.
  3. Final Answer:

    Flask redirects the user to /home/ automatically. -> Option C
  4. Quick Check:

    Trailing slash route redirects missing slash URL [OK]
Hint: Trailing slash routes redirect missing slash URLs [OK]
Common Mistakes:
  • Thinking Flask serves both URLs without redirect
  • Assuming 404 error occurs without slash
  • Believing Flask throws server error
2. Which of the following is the correct syntax to define a Flask route that does NOT accept a trailing slash?
easy
A. @app.route('/about/')
B. @app.route('/about//')
C. @app.route('about/')
D. @app.route('/about')

Solution

  1. 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.
  2. Step 2: Check each option

    @app.route('/about') uses '/about' without a trailing slash, which is correct syntax.
  3. Final Answer:

    @app.route('/about') -> Option D
  4. Quick Check:

    Route without trailing slash = no slash in route string [OK]
Hint: No trailing slash means route string ends without slash [OK]
Common Mistakes:
  • Adding extra slashes in route string
  • Omitting leading slash in route
  • Confusing trailing slash with double slashes
3. Given this Flask code:
@app.route('/page/')
def page():
    return 'Page with slash'

What will happen if a user visits /page in their browser?
medium
A. The user sees 'Page with slash' without URL change.
B. The user is redirected to /page/ and sees 'Page with slash'.
C. The user gets a 404 Not Found error.
D. The server crashes with an error.

Solution

  1. Step 1: Analyze route definition with trailing slash

    The route '/page/' expects URLs ending with a slash.
  2. Step 2: Understand Flask's redirect behavior

    If the user visits '/page' without the slash, Flask redirects to '/page/' automatically and serves the response.
  3. Final Answer:

    The user is redirected to /page/ and sees 'Page with slash'. -> Option B
  4. Quick Check:

    Trailing slash route redirects missing slash URL [OK]
Hint: Trailing slash route redirects missing slash URL [OK]
Common Mistakes:
  • Assuming no redirect happens
  • Expecting 404 error on missing slash
  • Thinking server crashes on missing slash
4. Consider this Flask code:
@app.route('/contact')
def contact():
    return 'Contact page'

What happens if a user visits /contact/ with a trailing slash?
medium
A. Flask returns a 404 Not Found error.
B. Flask throws a server error.
C. Flask serves the page normally.
D. Flask redirects to /contact without slash.

Solution

  1. Step 1: Identify route without trailing slash

    The route '/contact' is defined without a trailing slash.
  2. 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.
  3. Final Answer:

    Flask returns a 404 Not Found error. -> Option A
  4. Quick Check:

    No trailing slash route rejects extra slash URL [OK]
Hint: No trailing slash route rejects URLs with slash [OK]
Common Mistakes:
  • Thinking Flask redirects to no-slash URL
  • Assuming page loads normally with slash
  • Believing server throws error instead of 404
5. You want a single Flask route to accept both /blog and /blog/ URLs without redirecting or 404 errors. Which approach achieves this?
hard
A. Use Flask's strict_slashes=False option on the route decorator.
B. Define one route with trailing slash: @app.route('/blog/') only.
C. Define one route without trailing slash: @app.route('/blog') only.
D. Define two routes: @app.route('/blog') and @app.route('/blog/') pointing to the same function.

Solution

  1. Step 1: Understand default Flask behavior

    By default, Flask treats routes with and without trailing slash differently, redirecting or 404ing as needed.
  2. Step 2: Use strict_slashes=False to accept both

    Setting strict_slashes=False on the route decorator disables strict trailing slash matching, allowing both URLs without redirect or error.
  3. Final Answer:

    Use Flask's strict_slashes=False option on the route decorator. -> Option A
  4. Quick Check:

    strict_slashes=False allows both URLs [OK]
Hint: Use strict_slashes=False to accept both slash and no-slash URLs [OK]
Common Mistakes:
  • Defining two routes duplicates code
  • Relying on default redirect behavior
  • Ignoring strict_slashes option
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard