What is the purpose of the @app.route decorator in Flask?
The @app.route decorator tells Flask which URL should trigger a specific function. It connects a web address to the code that runs when that address is visited.
Click to reveal answer
beginner
How do you define a route for the home page using @app.route?
You use @app.route('/') above a function. This means when someone visits the root URL (like 'http://localhost:5000/'), Flask runs that function.
Click to reveal answer
intermediate
Can @app.route handle different HTTP methods? How?
Yes. You can add the methods argument like @app.route('/path', methods=['GET', 'POST']) to tell Flask which HTTP methods the route accepts.
Click to reveal answer
intermediate
What happens if two functions have the same route with @app.route?
Flask will use the last function defined for that route, overwriting the previous one. This can cause unexpected behavior, so routes should be unique.
Click to reveal answer
beginner
How can you capture a variable part of the URL using @app.route?
You use angle brackets in the route like @app.route('/user/<username>'). Flask passes the value in the URL to the function as an argument.
Click to reveal answer
What does @app.route('/about') do in a Flask app?
AImports the Flask module
BCreates a new Flask app
CStarts the Flask server
DLinks the URL '/about' to a function that runs when visited
✗ Incorrect
The route decorator connects the URL '/about' to a function that Flask runs when that URL is accessed.
How do you allow a route to accept both GET and POST requests?
AAdd <code>methods=['GET', 'POST']</code> in <code>@app.route</code>
BUse <code>@app.methods(['GET', 'POST'])</code>
CSet <code>app.methods = ['GET', 'POST']</code>
DFlask routes accept all methods by default
✗ Incorrect
You specify allowed HTTP methods by adding the methods argument in the route decorator.
What will Flask do if two functions have the same route?
AThrow an error and stop the app
BUse the last defined function for that route
CRun both functions in order
DIgnore the second function
✗ Incorrect
Flask overwrites the first function with the last one defined for the same route.
How do you capture a username from the URL in Flask?
Python requires the function body to be indented. Here, print and return are not indented under def hello():.
Step 2: Verify decorator and function syntax
The decorator has correct parentheses and the function name is valid, so no errors there.
Final Answer:
The function body is not indented properly. -> Option D
Quick Check:
Python needs indented function bodies [OK]
Hint: Indent function code under def line [OK]
Common Mistakes:
Forgetting to indent function body
Removing parentheses from decorator
Misnaming the function
5. You want to create a Flask route that accepts a username in the URL like /profile/john and returns Hello, john!. Which code correctly achieves this?
hard
A. @app.route('/profile/<username>')\ndef profile():\n return f'Hello, {username}!'
B. @app.route('/profile/')\ndef profile(username):\n return f'Hello, {username}!'
C. @app.route('/profile/<username>')\ndef profile(username):\n return f'Hello, {username}!'
D. @app.route('/profile/username')\ndef profile():\n return 'Hello, username!'
Solution
Step 1: Use variable URL part for username
The route must capture username as a string variable using <username> in the URL.
Step 2: Define function with matching parameter and return greeting
The function must accept username as a parameter and return the greeting using an f-string.
Final Answer:
@app.route('/profile/<username>')\ndef profile(username):\n return f'Hello, {username}!' -> Option C