0
0
Flaskframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Route with dynamic parameters
Client sends URL request
Flask checks route patterns
Match route with dynamic parameter?
No404 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.