A dynamic parameter is a part of the URL that can change and is captured by the route to pass as a variable to the view function. It allows routes to handle different inputs without defining multiple static routes.
Click to reveal answer
beginner
How do you define a route with a dynamic parameter in Flask?
Use angle brackets in the route URL, like <parameter_name>. For example, @app.route('/user/<username>') captures the username from the URL and passes it to the function.
Click to reveal answer
intermediate
What data types can you specify for dynamic parameters in Flask routes?
You can specify types like , , or . This helps Flask convert the URL part to the correct Python type before passing it to the function.
Click to reveal answer
intermediate
What happens if a dynamic parameter does not match the specified type in Flask?
Flask will return a 404 Not Found error because the URL does not match the route pattern with the expected type.
Click to reveal answer
beginner
Show a simple Flask route example with a dynamic parameter and explain its behavior.
Example:
@app.route('/post/')
def show_post(post_id):
return f"Post {post_id}"
This route captures an integer post_id from the URL and returns a string showing that post's ID.
Click to reveal answer
How do you capture a username from the URL in a Flask route?
A@app.route('/user/<username>')
B@app.route('/user/username')
C@app.route('/user/:username')
D@app.route('/user/{username}')
✗ Incorrect
Flask uses angle brackets < > to capture dynamic parameters in routes.
What type will Flask convert the parameter to if you use in the route?
ABoolean
BString
CFloat
DInteger
✗ Incorrect
The syntax tells Flask to convert the URL part to an integer.
What response does Flask give if the URL parameter does not match the expected type?
A500 Internal Server Error
B200 OK with empty content
C404 Not Found
D302 Redirect
✗ Incorrect
Flask returns a 404 Not Found if the URL does not match the route pattern.
Which of these is a valid dynamic parameter for a file path in Flask?
A<file>
B<path:file_path>
C<str:file_path>
D<url:file_path>
✗ Incorrect
The converter captures a path including slashes.
What is the purpose of dynamic parameters in Flask routes?
ATo capture parts of the URL as variables for the function
BTo create multiple routes with the same URL
CTo make routes static and unchangeable
DTo disable URL parameters
✗ Incorrect
Dynamic parameters let routes accept variable parts of the URL and pass them to the function.
Explain how to create a Flask route that accepts a dynamic username and returns a greeting message.
Think about how to capture and use the username from the URL.
You got /3 concepts.
Describe the role of type converters in Flask dynamic routes and give examples.
Consider why you might want to ensure a parameter is an integer.
You got /3 concepts.
Practice
(1/5)
1. What does the dynamic parameter in a Flask route do? @app.route('/user/<username>')
easy
A. It captures the part of the URL as a variable passed to the function.
B. It creates a fixed URL that never changes.
C. It makes the route only accept numbers.
D. It disables the route from working.
Solution
Step 1: Understand dynamic parameters in Flask routes
Dynamic parameters are parts of the URL wrapped in <> that Flask captures as variables.
Step 2: How Flask uses the captured variable
The captured part is passed as an argument to the view function to use inside the code.
Final Answer:
It captures the part of the URL as a variable passed to the function. -> Option A
Quick Check:
Dynamic parameter = variable in function [OK]
Hint: Dynamic parts in <> become function arguments [OK]
Common Mistakes:
Thinking dynamic means fixed URL
Assuming only numbers can be captured
Believing dynamic disables the route
2. Which of these is the correct syntax to define a route with an integer parameter named post_id?
easy
A. @app.route('/post/')
B. @app.route('/post/<post_id>')
C. @app.route('/post/')
D. @app.route('/post/')
Solution
Step 1: Identify the type syntax for dynamic parameters
Flask uses <type:name> to specify the parameter type, like int for integers.
Step 2: Match the correct syntax for integer parameter
The correct syntax is <int:post_id> to ensure only integers are accepted.
Final Answer:
@app.route('/post/<int:post_id>') -> Option A
Quick Check:
Integer type uses <int:name> [OK]
Hint: Use <int:name> for integer parameters in routes [OK]
Common Mistakes:
Omitting the type for integer parameters
Using str instead of int for numbers
Using float when integer is needed
3. What will be the output if you visit /product/42 given this Flask route?
B. Function parameter name does not match route parameter name.
C. Missing return statement in function.
D. Route URL should not have dynamic parameters.
Solution
Step 1: Compare route parameter and function argument names
The route defines <int:item_id> but the function uses parameter named item, which is inconsistent.
Step 2: Understand how Flask matches parameters
Flask passes the URL part as argument named item_id, so function must accept item_id to receive it correctly.
Final Answer:
Function parameter name does not match route parameter name. -> Option B
Quick Check:
Parameter names must match [OK]
Hint: Function argument must match route parameter name exactly [OK]
Common Mistakes:
Using different names for route and function parameters
Thinking type int is invalid syntax
Forgetting to return a value
5. You want a Flask route that accepts a username (string) and an optional integer page number with default 1. Which route and function definition is correct?
hard
A. @app.route('/profile/<username>/')
def profile(username, page=1):
return f"User: {username}, Page: {page}"
B. @app.route('/profile/<username>/<page>')
def profile(username, page=1):
return f"User: {username}, Page: {page}"
C. @app.route('/profile/<username>')
def profile(username, page=1):
return f"User: {username}, Page: {page}"
D. @app.route('/profile/<username>/')
def profile(username, page):
return f"User: {username}, Page: {page}"
Solution
Step 1: Understand optional parameters in Flask routes
Flask routes do not support optional URL parts directly; optional parameters must be handled with defaults in function and route without that part.
Step 2: Check which route allows optional page parameter
Route with only <username> works, and function uses default page=1 if not provided.