0
0
Flaskframework~10 mins

Blueprint URL prefixes in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Blueprint URL prefixes
Create Blueprint
Define Routes
Register Blueprint with app
Assign URL prefix
Run app
Request URL
Match prefix + route
Call route function
Return response
This flow shows how a Flask Blueprint is created, registered with a URL prefix, and how incoming requests match the prefix plus route to call the correct function.
Execution Sample
Flask
from flask import Flask, Blueprint

bp = Blueprint('bp', __name__, url_prefix='/admin')

@bp.route('/dashboard')
def dashboard():
    return 'Admin Dashboard'

app = Flask(__name__)
app.register_blueprint(bp)

if __name__ == '__main__':
    app.run()
Defines a Blueprint with URL prefix '/admin' and a route '/dashboard' that returns a string.
Execution Table
StepActionURL AccessedMatched URLFunction CalledResponse
1Create Blueprint 'bp' with prefix '/admin'N/AN/AN/AN/A
2Define route '/dashboard' on 'bp'N/AN/AN/AN/A
3Register 'bp' with appN/AN/AN/AN/A
4Access URL '/admin/dashboard'/admin/dashboard/admin/dashboarddashboard()'Admin Dashboard'
5Access URL '/dashboard'/dashboardNo match404 Not Found404 Not Found
💡 Execution stops after matching URL or returning 404 if no match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
bp.url_prefixNone/admin/admin/admin/admin/admin
app.blueprints{}{}{}{'bp': Blueprint}{'bp': Blueprint}{'bp': Blueprint}
request URLN/AN/AN/AN/A/admin/dashboard/dashboard
matched routeN/AN/AN/AN/A/admin/dashboardNo match
Key Moments - 2 Insights
Why does accessing '/dashboard' return 404 even though the route exists?
Because the Blueprint has a URL prefix '/admin', the full route is '/admin/dashboard'. Accessing '/dashboard' misses the prefix, so no route matches (see execution_table step 5).
How does the URL prefix affect route matching?
The prefix is added before all Blueprint routes when registered. So the app looks for URLs starting with the prefix plus the route path (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response do you get when accessing '/admin/dashboard'?
A'Admin Dashboard'
B404 Not Found
CError
DEmpty response
💡 Hint
Check execution_table row 4 under 'Response'
At which step does the app register the Blueprint with the URL prefix?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Register bp with app' in execution_table
If you remove the url_prefix from the Blueprint, what happens when accessing '/dashboard'?
AReturns 404 Not Found
BApp crashes
CReturns 'Admin Dashboard'
DReturns empty response
💡 Hint
Refer to variable_tracker for how prefix affects route matching
Concept Snapshot
Blueprint URL prefixes in Flask:
- Create Blueprint with url_prefix='/prefix'
- Define routes on Blueprint normally
- Register Blueprint with app
- Access routes with prefix + route path
- Requests without prefix won't match Blueprint routes
Full Transcript
In Flask, a Blueprint groups routes under a common URL prefix. First, you create a Blueprint and set a url_prefix like '/admin'. Then you define routes on this Blueprint, for example '/dashboard'. When you register the Blueprint with the main app, all its routes are accessed with the prefix added, so '/admin/dashboard' works. Accessing '/dashboard' alone does not match and returns 404. This helps organize routes logically and avoid conflicts. The execution table shows steps from Blueprint creation to route matching and response. The variable tracker shows how the url_prefix and request URL change during execution.