What is the purpose of static files in a Flask Blueprint?
Static files are resources like images, CSS, and JavaScript that do not change dynamically. In a Flask Blueprint, static files allow you to organize these resources separately for each part of your app.
Click to reveal answer
beginner
How do you specify a folder for static files in a Flask Blueprint?
When creating a Blueprint, you use the 'static_folder' parameter to tell Flask where to find the static files for that Blueprint.
Click to reveal answer
intermediate
What URL path prefix does Flask use by default for Blueprint static files?
By default, Flask serves Blueprint static files under '/<blueprint_name>/static/'. For example, if the Blueprint is named 'admin', static files are accessed at '/admin/static/'.
Click to reveal answer
beginner
How can you link to a Blueprint's static file in a Jinja2 template?
Use the 'url_for' function with the format 'url_for("<blueprint_name>.static", filename="file_name")'. This generates the correct URL to the static file inside the Blueprint.
Click to reveal answer
intermediate
Can multiple Blueprints have their own static folders in Flask?
Yes, each Blueprint can have its own static folder. This helps keep static files organized by feature or section of the app.
Click to reveal answer
How do you tell Flask where a Blueprint's static files are located?
ABy registering static files in the app config
BBy adding static files to the main app folder
CBy using the 'static_url_path' parameter only
DBy setting the 'static_folder' parameter when creating the Blueprint
✗ Incorrect
The 'static_folder' parameter in the Blueprint constructor specifies the folder for static files.
What URL path does Flask use to serve static files from a Blueprint named 'shop' by default?
A/shop/static/
B/static/
C/static/shop/
D/shop/
✗ Incorrect
Flask serves Blueprint static files under '//static/', so for 'shop' it is '/shop/static/'.
Which function helps you generate the URL for a Blueprint's static file in a template?
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