0
0
Flaskframework~5 mins

Route decorator (@app.route) in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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
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
How do you capture a username from the URL in Flask?
AUse <code>@app.route('/user/&lt;username&gt;')</code>
BUse <code>@app.route('/user/:username')</code>
CUse <code>@app.route('/user/username')</code>
DUse <code>@app.route('/user?username')</code>
What URL does @app.route('/') represent?
AA 404 error page
BThe about page
CThe home or root page
DA static file
Explain how the @app.route decorator works in Flask and how it connects URLs to functions.
Think about how you tell Flask what to do when someone visits a web page.
You got /4 concepts.
    Describe how to create a dynamic route in Flask that accepts a username from the URL.
    Imagine you want to greet different users by name in the URL.
    You got /4 concepts.