Bird
Raised Fist0
Flaskframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a URL prefix in a Flask blueprint?
easy
A. To add a common path segment to all routes in the blueprint
B. To change the HTTP methods allowed for the routes
C. To register the blueprint with the Flask app
D. To define the template folder for the blueprint

Solution

  1. Step 1: Understand what URL prefix means in Flask blueprints

    A URL prefix is a string added before all routes in a blueprint to group them under a common path.
  2. Step 2: Identify the purpose of URL prefix

    This helps organize routes logically and keeps URLs clean by adding a shared path segment.
  3. Final Answer:

    To add a common path segment to all routes in the blueprint -> Option A
  4. Quick Check:

    URL prefix = common path for blueprint routes [OK]
Hint: URL prefix groups routes under one shared path [OK]
Common Mistakes:
  • Confusing URL prefix with HTTP methods
  • Thinking URL prefix registers the blueprint
  • Mixing URL prefix with template folder settings
2. Which of the following is the correct way to set a URL prefix when creating a Flask blueprint?
easy
A. bp = Blueprint('bp', __name__, urlprefix='/admin')
B. bp = Blueprint('bp', __name__, prefix_url='/admin')
C. bp = Blueprint('bp', __name__, url_prefix='/admin')
D. bp = Blueprint('bp', __name__, route_prefix='/admin')

Solution

  1. Step 1: Recall the correct parameter name for URL prefix

    The Flask Blueprint constructor uses the parameter named 'url_prefix' to set the prefix.
  2. Step 2: Check each option for correct syntax

    Only bp = Blueprint('bp', __name__, url_prefix='/admin') uses 'url_prefix' exactly as Flask expects.
  3. Final Answer:

    bp = Blueprint('bp', __name__, url_prefix='/admin') -> Option C
  4. Quick Check:

    Correct parameter name is url_prefix [OK]
Hint: Use 'url_prefix' exactly when creating blueprint [OK]
Common Mistakes:
  • Using incorrect parameter names like prefix_url
  • Misspelling url_prefix as urlprefix
  • Confusing route_prefix with url_prefix
3. Given this Flask blueprint code:
bp = Blueprint('bp', __name__, url_prefix='/shop')

@bp.route('/items')
def items():
    return 'Shop Items'

What URL path will show the text 'Shop Items' when this blueprint is registered?
medium
A. /shop/items
B. /bp/items
C. /items
D. /shop/bp/items

Solution

  1. Step 1: Understand how url_prefix affects route paths

    The url_prefix '/shop' is added before all routes in the blueprint.
  2. Step 2: Combine prefix with route path

    The route '/items' becomes '/shop/items' when accessed in the app.
  3. Final Answer:

    /shop/items -> Option A
  4. Quick Check:

    URL = url_prefix + route path = /shop/items [OK]
Hint: URL = url_prefix + route path [OK]
Common Mistakes:
  • Ignoring the url_prefix and using only route path
  • Assuming blueprint name appears in URL
  • Adding extra segments not defined
4. What is wrong with this Flask blueprint code?
bp = Blueprint('bp', __name__, url_prefix='/api')

@bp.route('data')
def data():
    return 'API Data'
medium
A. The function 'data' must accept a parameter
B. The url_prefix should be set when registering the blueprint, not here
C. The blueprint name 'bp' is invalid
D. The route path is missing a leading slash '/'

Solution

  1. Step 1: Check the route decorator syntax

    Flask route paths must start with a leading slash '/' to be valid.
  2. Step 2: Identify the missing slash in '@bp.route('data')'

    The route should be '@bp.route('/data')' to work correctly.
  3. Final Answer:

    The route path is missing a leading slash '/' -> Option D
  4. Quick Check:

    Route paths need leading '/' [OK]
Hint: Always start route paths with '/' [OK]
Common Mistakes:
  • Omitting leading slash in route paths
  • Thinking url_prefix is set only on registration
  • Believing blueprint names have strict rules
  • Assuming route functions need parameters without reason
5. You have two blueprints:
bp1 = Blueprint('bp1', __name__, url_prefix='/user')
@bp1.route('/profile')
def profile():
    return 'User Profile'

bp2 = Blueprint('bp2', __name__, url_prefix='/user')
@bp2.route('/settings')
def settings():
    return 'User Settings'

If both are registered in the app, what happens when you visit /user/settings?
hard
A. A routing conflict error occurs because both blueprints share the same prefix
B. The 'User Settings' page is shown correctly
C. The 'User Profile' page is shown instead
D. A 404 Not Found error occurs

Solution

  1. Step 1: Understand blueprint URL prefixes and route paths

    Both blueprints share the same prefix '/user', but their routes differ: '/profile' and '/settings'.
  2. Step 2: Check if routes overlap

    Since '/user/settings' matches only bp2's route, no conflict occurs.
  3. Final Answer:

    The 'User Settings' page is shown correctly -> Option B
  4. Quick Check:

    Same prefix allowed if routes differ [OK]
Hint: Same prefix okay if route paths differ [OK]
Common Mistakes:
  • Assuming same prefix always causes conflict
  • Confusing blueprint name with URL path
  • Expecting 404 when route exists
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