Bird
Raised Fist0
Flaskframework~10 mins

Route with dynamic parameters in Flask - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Route with dynamic parameters
Client sends URL request
↓
Flask checks route patterns
↓
Match route with dynamic parameter?
No→404 Not Found
Yes↓
Extract parameter value from URL
↓
Call view function with parameter
↓
Return response with parameter used
Flask receives a URL, matches it to a route pattern with a dynamic part, extracts that part, and passes it to the function to create a response.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/user/<username>')
def show_user(username):
    return f"User: {username}"
Defines a route that captures the username from the URL and returns it in the response.
Execution Table
StepURL RequestedRoute Pattern MatchedParameter ExtractedFunction CalledResponse
1/user/alice/user/<username>aliceshow_user('alice')User: alice
2/user/bob/user/<username>bobshow_user('bob')User: bob
3/user/No match--404 Not Found
4/profile/aliceNo match--404 Not Found
💡 Execution stops when a matching route is found and response returned, or 404 if no match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
username-alicebob--
response-User: aliceUser: bob404 Not Found404 Not Found
Key Moments - 2 Insights
Why does /user/ (with no username) cause a 404 error?
Because the route expects a dynamic parameter after /user/, as shown in execution_table step 3, no parameter means no match.
How does Flask know what part of the URL is the username?
Flask uses the <username> placeholder in the route pattern to capture that part of the URL, as seen in steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response when the URL '/user/bob' is requested?
A404 Not Found
BUser: alice
CUser: bob
DError
💡 Hint
Check row 2 under the Response column in the execution_table.
At which step does the URL not match any route pattern?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 'No match' in the Route Pattern Matched column in execution_table.
If the route was changed to '/profile/<username>', what would happen when requesting '/user/alice'?
AIt matches and returns 'User: alice'
BIt returns 404 Not Found
CIt matches but returns an error
DIt redirects to /profile/alice
💡 Hint
Refer to execution_table step 4 where '/profile/alice' does not match '/user/'.
Concept Snapshot
Flask routes can include dynamic parts using <parameter> syntax.
When a URL matches, Flask extracts the parameter value.
The value is passed as an argument to the view function.
If no route matches, Flask returns a 404 error.
Example: @app.route('/user/<username>') captures username from URL.
Full Transcript
In Flask, routes can have dynamic parameters marked by angle brackets like <username>. When a client requests a URL, Flask checks if it matches any route pattern. If it matches a route with a dynamic parameter, Flask extracts the value from the URL and passes it to the function. The function then uses this value to create a response. If no route matches, Flask returns a 404 error. For example, the route '/user/<username>' matches URLs like '/user/alice' and passes 'alice' to the function. URLs without the parameter or with a different path do not match and cause 404 errors.

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

  1. Step 1: Understand dynamic parameters in Flask routes

    Dynamic parameters are parts of the URL wrapped in <> that Flask captures as variables.
  2. 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.
  3. Final Answer:

    It captures the part of the URL as a variable passed to the function. -> Option A
  4. 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

  1. Step 1: Identify the type syntax for dynamic parameters

    Flask uses <type:name> to specify the parameter type, like int for integers.
  2. Step 2: Match the correct syntax for integer parameter

    The correct syntax is <int:post_id> to ensure only integers are accepted.
  3. Final Answer:

    @app.route('/post/<int:post_id>') -> Option A
  4. 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?
@app.route('/product/')
def product(id):
    return f"Product ID: {id}"
medium
A. 404 Not Found error
B. Product ID:
C. Product ID: id
D. Product ID: 42

Solution

  1. Step 1: Understand the route and parameter type

    The route expects an integer parameter named id, which matches 42 in the URL.
  2. Step 2: Check the function return value

    The function returns a string with the id value inserted, so it returns "Product ID: 42".
  3. Final Answer:

    Product ID: 42 -> Option D
  4. Quick Check:

    Dynamic int parameter outputs value [OK]
Hint: Dynamic int parameter passes number as function argument [OK]
Common Mistakes:
  • Expecting the literal string instead of value
  • Confusing parameter name with string
  • Assuming route returns error for valid int
4. Identify the error in this Flask route definition:
@app.route('/item/')
def item(item):
    return f"Item {item_id}"
medium
A. Route parameter type int is invalid.
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

  1. 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.
  2. 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.
  3. Final Answer:

    Function parameter name does not match route parameter name. -> Option B
  4. 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

  1. 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.
  2. Step 2: Check which route allows optional page parameter

    Route with only <username> works, and function uses default page=1 if not provided.
  3. Final Answer:

    @app.route('/profile/<username>')\ndef profile(username, page=1):\n return f"User: {username}, Page: {page}" -> Option C
  4. Quick Check:

    Optional params handled by function defaults, not route [OK]
Hint: Optional URL parts need separate routes or defaults in function [OK]
Common Mistakes:
  • Assuming Flask supports optional URL parts in one route
  • Using default values with required route parameters
  • Mismatching parameter types in route and function
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