Bird
Raised Fist0
Flaskframework~20 mins

Why routing is Flask's core - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flask Routing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Flask route return?
Given the Flask app code below, what will the browser display when visiting '/'?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Welcome to Flask!'

if __name__ == '__main__':
    app.run()
AA blank page
B404 Not Found error
CWelcome to Flask!
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Look at the function decorated with @app.route('/') and what it returns.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Flask route
Which option shows the correct way to define a route in Flask without syntax errors?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello!'

if __name__ == '__main__':
    app.run()
A@app.route('/hello')\ndef hello():\n return 'Hello!'
B@app.route('/hello')\ndef hello():\nreturn 'Hello!'
C@app.route('/hello')\ndef hello()\n return 'Hello!'
D@app.route('/hello')\nhello():\n return 'Hello!'
Attempts:
2 left
💡 Hint
Check indentation and colons after function definitions.
❓ state_output
advanced
2:00remaining
What is the output when accessing a dynamic route?
Consider this Flask app code. What will be the output when visiting '/user/Alice' in the browser?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/user/<name>')
def user(name):
    return f'Hello, {name}!'

if __name__ == '__main__':
    app.run()
AHello, Alice!
BHello, <name>!
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Look at how the route uses and how the function uses that parameter.
🔧 Debug
advanced
2:00remaining
Why does this Flask route cause a 404 error?
Given the code below, why does visiting '/about' in the browser show a 404 error?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/about/', strict_slashes=True)
def about():
    return 'About page'

if __name__ == '__main__':
    app.run()
ABecause the function 'about' does not return a string
BBecause the route is defined with a trailing slash '/about/', but the URL visited is '/about' without the slash
CBecause Flask requires routes to be uppercase
DBecause the app.run() is missing debug=true
Attempts:
2 left
💡 Hint
Check the exact route string and the URL visited.
🧠 Conceptual
expert
3:00remaining
Why is routing considered the core of Flask?
Which option best explains why routing is the core concept in Flask?
ARouting handles database connections and queries automatically
BRouting styles the HTML pages with CSS and JavaScript
CRouting manages user authentication and sessions by default
DRouting connects URLs to Python functions, defining how the app responds to web requests
Attempts:
2 left
💡 Hint
Think about what happens when a user visits a web address in a Flask app.

Practice

(1/5)
1. Why is routing considered the core of Flask?
easy
A. Because it connects web addresses (URLs) to Python functions that handle requests.
B. Because it manages the database connections automatically.
C. Because it styles the webpage with CSS.
D. Because it handles user authentication by default.

Solution

  1. Step 1: Understand Flask's main job

    Flask is a web framework that responds to web requests by running Python code.
  2. Step 2: Role of routing in Flask

    Routing links URLs to specific Python functions, so Flask knows what code to run for each web address.
  3. Final Answer:

    Because it connects web addresses (URLs) to Python functions that handle requests. -> Option A
  4. Quick Check:

    Routing = URL to function link [OK]
Hint: Routing links URLs to functions in Flask [OK]
Common Mistakes:
  • Thinking routing manages databases
  • Confusing routing with styling or CSS
  • Assuming routing handles user login automatically
2. Which of the following is the correct way to define a route in Flask?
easy
A. @app.route('/home')\nreturn 'Welcome!'
B. @app.route('/home')\ndef home():\n return 'Welcome!'
C. def app.route('/home'):\n return 'Welcome!'
D. app.route('/home')\ndef home():\n print('Welcome!')

Solution

  1. Step 1: Recognize Flask route decorator syntax

    Routes use the @app.route decorator above a function to link a URL to that function.
  2. Step 2: Check each option's syntax

    @app.route('/home')\ndef home():\n return 'Welcome!' correctly uses @app.route('/home') above a function that returns a string. Others have syntax errors or missing function definitions.
  3. Final Answer:

    @app.route('/home')\ndef home():\n return 'Welcome!' -> Option B
  4. Quick Check:

    Route decorator + function = correct syntax [OK]
Hint: Use @app.route above a function to define routes [OK]
Common Mistakes:
  • Missing @ before app.route
  • Placing return outside function
  • Using app.route as a function instead of decorator
3. What will be the output when accessing the URL /user/42 in this Flask app?
@app.route('/user/<int:id>')
def show_user(id):
    return f'User ID is {id}'
medium
A. User ID is 42
B. User ID is <int:id>
C. 404 Not Found error
D. User ID is id

Solution

  1. Step 1: Understand dynamic routing with variables

    The route uses <int:id> to capture an integer from the URL and pass it as the 'id' argument.
  2. Step 2: Check the function output

    The function returns a string with the captured id inserted using an f-string, so accessing /user/42 returns 'User ID is 42'.
  3. Final Answer:

    User ID is 42 -> Option A
  4. Quick Check:

    Dynamic route passes 42 as id [OK]
Hint: Dynamic routes pass URL parts as function inputs [OK]
Common Mistakes:
  • Thinking the URL part is a literal string
  • Expecting a 404 error for valid route
  • Not using f-string to insert variable
4. Identify the error in this Flask route code:
@app.route('/hello')
def hello():
print('Hello World')
return 'Hello!'
medium
A. Return statement cannot be used in Flask routes.
B. Route decorator syntax is incorrect.
C. Missing indentation inside the function.
D. Function name 'hello' is invalid.

Solution

  1. Step 1: Check Python indentation rules

    Python requires code inside functions to be indented. Here, 'print' and 'return' are not indented.
  2. Step 2: Confirm route decorator and function name

    The decorator syntax and function name are valid. Return statements are allowed in Flask routes.
  3. Final Answer:

    Missing indentation inside the function. -> Option C
  4. Quick Check:

    Indent code inside functions [OK]
Hint: Indent function code properly in Python [OK]
Common Mistakes:
  • Ignoring indentation errors
  • Thinking decorator syntax is wrong
  • Believing return is disallowed in routes
5. You want to create a Flask route that accepts a username in the URL and returns a greeting only if the username is not empty. Which code correctly implements this?
hard
A. @app.route('/greet/<username>') def greet(username): return 'Hello!'
B. @app.route('/greet/<username>') def greet(username): if username == None: return 'No username provided.' return f'Hello, {username}!'
C. @app.route('/greet/') def greet(): return 'Hello, user!'
D. @app.route('/greet/<username>') def greet(username): if username: return f'Hello, {username}!' else: return 'No username provided.'

Solution

  1. Step 1: Understand dynamic route with username

    The route captures 'username' from the URL and passes it to the function.
  2. Step 2: Check conditional greeting logic

    @app.route('/greet/<username>') def greet(username): if username: return f'Hello, {username}!' else: return 'No username provided.' checks if username is truthy (not empty) and returns a personalized greeting; else returns a message for missing username.
  3. Step 3: Verify other options

    @app.route('/greet/<username>') def greet(username): if username == None: return 'No username provided.' return f'Hello, {username}!' incorrectly checks for None but username will never be None if route matches. @app.route('/greet/') def greet(): return 'Hello, user!' lacks username parameter. @app.route('/greet/<username>') def greet(username): return 'Hello!' ignores username value.
  4. Final Answer:

    @app.route('/greet/<username>') def greet(username): if username: return f'Hello, {username}!' else: return 'No username provided.' -> Option D
  5. Quick Check:

    Check username truthiness before greeting [OK]
Hint: Check if username is truthy to handle empty input [OK]
Common Mistakes:
  • Checking username == None instead of truthiness
  • Ignoring username parameter in function
  • Defining route without username parameter
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