A Blueprint in Flask is a way to organize a group of related routes and templates. It helps split a large app into smaller parts, like chapters in a book, making the app easier to manage.
Click to reveal answer
beginner
How do you register a Blueprint in a Flask app?
You create a Blueprint object, define routes on it, then register it on the main Flask app using app.register_blueprint(blueprint). This connects the Blueprint's routes to the app.
Click to reveal answer
intermediate
How do Blueprints help with templates in Flask?
Blueprints can have their own templates folder. When rendering templates inside a Blueprint, Flask looks in that folder first, keeping templates organized by feature or section.
Click to reveal answer
intermediate
What is the purpose of the 'url_prefix' parameter when registering a Blueprint?
The 'url_prefix' adds a common path before all routes in the Blueprint. For example, if url_prefix='/blog', all routes in that Blueprint start with '/blog', like '/blog/post1'.
Click to reveal answer
intermediate
Can Blueprints be used to share static files in Flask?
Yes, Blueprints can have their own static folder. This lets you organize static files like images or CSS by feature, and Flask serves them under the Blueprint's static path.
Click to reveal answer
What is the main benefit of using Blueprints in Flask?
AMake the app run faster
BOrganize routes and templates into reusable parts
CAutomatically create database tables
DReplace the Flask app object
✗ Incorrect
Blueprints help organize routes and templates into smaller, reusable parts, making the app easier to manage.
How do you tell Flask to use a Blueprint's routes?
ABy setting a config variable
BBy importing the Blueprint only
CBy calling app.register_blueprint(blueprint)
DBy adding routes to app directly
✗ Incorrect
You must register the Blueprint on the Flask app with app.register_blueprint() to activate its routes.
If a Blueprint has url_prefix='/shop', what URL will the route @bp.route('/cart') respond to?
A/cart
B/shop
C/cart/shop
D/shop/cart
✗ Incorrect
The url_prefix '/shop' is added before all Blueprint routes, so '/cart' becomes '/shop/cart'.
Where does Flask look for templates when rendering inside a Blueprint?
AIn the Blueprint's templates folder first
BOnly in the main app's templates folder
CIn the static folder
DIn the root directory
✗ Incorrect
Flask checks the Blueprint's templates folder first, then the main app's templates folder.
Can Blueprints have their own static files?
AYes, Blueprints can have a static folder
BNo, static files must be in the main app
COnly if you use a plugin
DStatic files are not supported in Blueprints
✗ Incorrect
Blueprints can have their own static folder to organize static files by feature.
Explain how Blueprints help organize routes and templates in a Flask app.
Think of Blueprints like chapters in a book.
You got /4 concepts.
Describe the role of the url_prefix parameter when registering a Blueprint.
It’s like a folder name for URLs.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a Blueprint in a Flask application?
easy
A. To handle user authentication automatically
B. To organize routes and templates into reusable components
C. To create a new Flask app instance
D. To connect Flask with a database
Solution
Step 1: Understand Blueprint role
A Blueprint groups related routes and templates to keep code organized.
Step 2: Compare other options
Connecting to databases, creating app instances, or handling authentication are separate tasks.
Final Answer:
To organize routes and templates into reusable components -> Option B
Quick Check:
Blueprint = organize routes/templates [OK]
Hint: Blueprints group routes/templates for better app structure [OK]
2. Which of the following is the correct way to register a Blueprint named blog with a Flask app app using the URL prefix /blog?
easy
A. app.register_blueprint(blog, url_prefix='/blog')
B. app.register_blueprint('/blog', blog)
C. app.register_blueprint(blog.prefix('/blog'))
D. app.register_blueprint(blog, prefix_url='/blog')
Solution
Step 1: Recall correct method signature
The method is register_blueprint(blueprint, url_prefix='...').
Step 2: Check options for correct syntax
Only app.register_blueprint(blog, url_prefix='/blog') uses the correct method and parameter names.
Final Answer:
app.register_blueprint(blog, url_prefix='/blog') -> Option A
Quick Check:
register_blueprint with url_prefix = app.register_blueprint(blog, url_prefix='/blog') [OK]
Hint: Use url_prefix parameter to set Blueprint URL base [OK]
Common Mistakes:
Swapping parameter order
Using wrong parameter name like prefix_url
Calling prefix as a method on Blueprint
3. Given this Blueprint code:
from flask import Blueprint, render_template
blog = Blueprint('blog', __name__, template_folder='templates')
@blog.route('/post')
def show_post():
return render_template('post.html')
And this main app code:
from flask import Flask
app = Flask(__name__)
app.register_blueprint(blog, url_prefix='/blog')
What URL should you visit to see the post.html template rendered?
medium
A. /blog
B. /post
C. /blog/post
D. /post/blog
Solution
Step 1: Understand route and url_prefix
The Blueprint route is '/post', and the Blueprint is registered with prefix '/blog'.
Step 2: Combine prefix and route
The full URL becomes '/blog/post' to access the route.
Final Answer:
/blog/post -> Option C
Quick Check:
url_prefix + route = /blog/post [OK]
Hint: Combine url_prefix and route for full URL [OK]
Common Mistakes:
Ignoring url_prefix and using only route
Swapping url_prefix and route order
Assuming url_prefix is not added to route
4. What is wrong with this Blueprint registration code?
from flask import Flask
app = Flask(__name__)
app.register_blueprint(blog, prefix='/blog')
medium
A. You cannot register blueprints in Flask
B. Blueprint must be registered before app creation
C. Blueprint name must be 'app' to register
D. The parameter should be url_prefix, not prefix
Solution
Step 1: Check parameter name in register_blueprint
The correct parameter to set URL prefix is url_prefix, not prefix.
Step 2: Validate other options
Blueprints register after app creation; name can be anything; Flask supports blueprints.
Final Answer:
The parameter should be url_prefix, not prefix -> Option D
Quick Check:
Correct param is url_prefix [OK]
Hint: Use url_prefix, not prefix, when registering Blueprint [OK]
Common Mistakes:
Using prefix instead of url_prefix
Trying to register Blueprint before app exists
Thinking Blueprint name must be 'app'
5. You have two Blueprints, admin and user, both with a route /dashboard. How can you register them in your Flask app to avoid URL conflicts and keep URLs clear?
hard
A. Register each Blueprint with a different url_prefix, like /admin and /user
B. Rename one Blueprint's route to /dashboard2
C. Register both Blueprints without any url_prefix
D. Use the same url_prefix for both Blueprints
Solution
Step 1: Understand URL conflicts
Both Blueprints have the same route /dashboard, which causes conflicts if no prefix is used.
Step 2: Use url_prefix to separate routes
Assigning different url_prefix values like /admin and /user keeps URLs unique and organized.
Final Answer:
Register each Blueprint with a different url_prefix, like /admin and /user -> Option A
Quick Check:
Different url_prefix avoids route conflicts [OK]
Hint: Use unique url_prefix for each Blueprint to avoid conflicts [OK]
Common Mistakes:
Not using url_prefix and causing route clashes
Renaming routes manually instead of using prefixes
Using same url_prefix for multiple Blueprints
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.