📖 Scenario: You are building a Flask web application that uses a blueprint to organize routes and static files separately. You want to serve static files like CSS and images from the blueprint folder.
🎯 Goal: Create a Flask blueprint named shop that serves static files from a folder called static_shop. Then register this blueprint in the main Flask app so that the static files are accessible under the URL prefix /shop/static.
📋 What You'll Learn
Create a Flask blueprint named shop with static folder static_shop
Set the static URL path of the blueprint to /shop/static
Register the shop blueprint in the main Flask app
Ensure static files are served correctly from the blueprint's static folder
💡 Why This Matters
🌍 Real World
Web applications often use blueprints to organize code and static files for different parts of the site, like admin panels or user areas.
💼 Career
Understanding blueprints and static file management is essential for building scalable Flask applications in professional web development.
Progress0 / 4 steps
1
Create the Flask blueprint with static folder
Create a Flask blueprint called shop with the static folder set to static_shop and the static URL path set to /shop/static.
Flask
Hint
Use Blueprint constructor with parameters static_folder and static_url_path.
2
Create the main Flask app
Create a Flask app instance called app using Flask(__name__).
Flask
Hint
Use Flask(__name__) to create the app instance.
3
Register the blueprint with the app
Register the shop blueprint with the app using app.register_blueprint(shop).
Flask
Hint
Use app.register_blueprint(shop) to connect the blueprint to the app.
4
Add a route to the blueprint
Add a route / to the shop blueprint that returns the string 'Welcome to the Shop!'.
Flask
Hint
Use @shop.route('/') decorator and define a function that returns the welcome string.
Practice
(1/5)
1. What is the main purpose of setting static_folder in a Flask blueprint?
easy
A. To specify the folder where the blueprint's static files are stored
B. To define the URL prefix for all routes in the blueprint
C. To register the blueprint with the Flask app
D. To set the template folder for the blueprint
Solution
Step 1: Understand the role of static_folder
The static_folder parameter tells Flask where to find static files like CSS or images for that blueprint.
Step 2: Differentiate from other parameters
It does not affect routes or templates; it only points to the static files directory.
Final Answer:
To specify the folder where the blueprint's static files are stored -> Option A
Quick Check:
static_folder = static files location [OK]
Hint: static_folder always points to static files folder [OK]
Common Mistakes:
Confusing static_folder with template_folder
Thinking static_folder sets URL prefix
Assuming static_folder registers blueprint
2. Which of the following is the correct way to create a Flask blueprint with static files located in a folder named assets?
easy
A. Blueprint('bp', __name__, static_folder='assets')
B. Blueprint('bp', __name__, static_url_path='assets')
C. Blueprint('bp', __name__, template_folder='assets')
D. Blueprint('bp', __name__, static_folder='/assets')
Solution
Step 1: Identify correct parameter for static files folder
The static_folder parameter sets the folder name relative to the blueprint.
Step 2: Check syntax correctness
Blueprint('bp', __name__, static_folder='assets') correctly uses static_folder='assets' without a leading slash, which is correct.
Final Answer:
Blueprint('bp', __name__, static_folder='assets') -> Option A
Quick Check:
static_folder='assets' is correct syntax [OK]
Hint: Use static_folder='foldername' without leading slash [OK]
Common Mistakes:
Using static_url_path instead of static_folder for folder name
Adding leading slash in static_folder path
Confusing template_folder with static_folder
3. Given this blueprint setup:
bp = Blueprint('bp', __name__, static_folder='static', static_url_path='/bp_static')
What URL will serve the file logo.png located inside the static folder of this blueprint?
medium
A. /static/logo.png
B. /bp_static/logo.png
C. /bp/logo.png
D. /bp_static/static/logo.png
Solution
Step 1: Understand static_url_path usage
The static_url_path sets the URL prefix for serving static files of the blueprint.
Step 2: Combine URL prefix with filename
Since static_url_path='/bp_static', the file logo.png is served at /bp_static/logo.png.
Final Answer:
/bp_static/logo.png -> Option B
Quick Check:
static_url_path + filename = URL [OK]
Hint: static_url_path sets URL prefix for blueprint static files [OK]
When running the app, you get a BuildError. What is the likely cause?
medium
A. The filename logo.png does not exist
B. The static_folder path is incorrect
C. The url_for call should use 'static' instead of 'bp.static'
D. The blueprint bp was not registered with the Flask app
Solution
Step 1: Understand BuildError in url_for
A BuildError means Flask cannot find the endpoint named bp.static.
Step 2: Check blueprint registration
If the blueprint bp is not registered with the app, its static endpoint won't exist, causing the error.
Final Answer:
The blueprint bp was not registered with the Flask app -> Option D
Quick Check:
Unregistered blueprint causes BuildError [OK]
Hint: Always register blueprint before using its static endpoint [OK]
Common Mistakes:
Forgetting to register blueprint with app
Using wrong endpoint name in url_for
Assuming file existence causes BuildError
5. You want to serve static files from two blueprints: bp1 with static files in static1 folder and URL prefix /static1, and bp2 with static files in static2 folder and URL prefix /static2. How do you correctly set up both blueprints to avoid URL conflicts and serve files properly?
hard
A. Set static_folder only for bp1 and leave bp2 without static files.
B. Use the same static_url_path for both blueprints but different static_folder names.
C. Set static_folder='static1' and static_url_path='/static1' for bp1, and static_folder='static2' and static_url_path='/static2' for bp2, then register both blueprints.
D. Register both blueprints without setting static_url_path to use default static URL.
Solution
Step 1: Avoid URL conflicts by setting unique static_url_path
Each blueprint must have a unique static_url_path to prevent URL clashes.
Step 2: Assign correct static_folder for each blueprint
Set static_folder to the folder containing that blueprint's static files.
Step 3: Register both blueprints with the app
Only after registration will the static files be served at their URL prefixes.
Final Answer:
Set static_folder='static1' and static_url_path='/static1' for bp1, and static_folder='static2' and static_url_path='/static2' for bp2, then register both blueprints. -> Option C