Bird
Raised Fist0
Flaskframework~20 mins

Blueprint creation and registration in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Blueprint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing the registered blueprint route?
Given the Flask app with a blueprint registered under '/shop', what will be the response when visiting '/shop/items'?
Flask
from flask import Flask, Blueprint

shop_bp = Blueprint('shop', __name__)

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

app = Flask(__name__)
app.register_blueprint(shop_bp, url_prefix='/shop')

# What is the output of a GET request to '/shop/items'?
AShop Items List
B404 Not Found
CInternal Server Error
DMethod Not Allowed
Attempts:
2 left
💡 Hint
Think about how url_prefix affects blueprint routes.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates and registers a blueprint?
Select the code snippet that correctly creates a Flask blueprint named 'admin' and registers it with the app under '/admin'.
A
admin_bp = Blueprint('admin')
app = Flask(__name__)
app.register_blueprint(admin_bp, url_prefix='/admin')
B
admin_bp = Blueprint('admin', __name__)
app = Flask(__name__)
app.register_blueprint(admin_bp, url_prefix='/admin')
C
admin_bp = Blueprint('admin', __name__)
app = Flask(__name__)
app.register_blueprint(admin_bp, prefix='/admin')
D
admin_bp = Blueprint('admin', __name__)
app = Flask(__name__)
app.register_blueprint(admin_bp, url='/admin')
Attempts:
2 left
💡 Hint
Check the Blueprint constructor and register_blueprint parameters.
🔧 Debug
advanced
2:00remaining
Why does this blueprint route cause a 404 error?
Consider this code snippet: from flask import Flask, Blueprint api = Blueprint('api', __name__) @api.route('/data') def data(): return 'API Data' app = Flask(__name__) # Missing blueprint registration # What happens when you visit '/data' and why?
A200 OK with response 'API Data' because blueprint routes are auto-registered
B500 Internal Server Error due to missing route handler
C404 Not Found because the blueprint was never registered with the app
D405 Method Not Allowed because GET is not allowed
Attempts:
2 left
💡 Hint
Think about what happens if you define a blueprint but don't register it.
❓ state_output
advanced
2:00remaining
What is the output of this blueprint route using url variables?
Given the blueprint and route below, what is the output when visiting '/blog/post/42'?
Flask
from flask import Flask, Blueprint

blog_bp = Blueprint('blog', __name__)

@blog_bp.route('/post/<int:post_id>')
def show_post(post_id):
    return f'Post ID: {post_id}'

app = Flask(__name__)
app.register_blueprint(blog_bp, url_prefix='/blog')
APost ID: post/42
BPost ID: <int:post_id>
C404 Not Found
DPost ID: 42
Attempts:
2 left
💡 Hint
Look at how url variables are passed to the route function.
🧠 Conceptual
expert
3:00remaining
What happens if two blueprints have overlapping routes without url_prefix?
If you create two blueprints, both defining a route for '/' and register both without url_prefix, what will happen when you visit '/' in the Flask app?
AThe route from the blueprint registered last will override the first, so only one response is served
BBoth routes will be merged and served together in the response
CFlask will raise a runtime error due to route conflict
DBoth routes will be served alternately on each request
Attempts:
2 left
💡 Hint
Think about how Flask handles route conflicts in registration order.

Practice

(1/5)
1. What is the main purpose of using a Blueprint in a Flask application?
easy
A. To handle user authentication
B. To create database models automatically
C. To organize routes and related code into reusable modules
D. To serve static files directly

Solution

  1. Step 1: Understand Blueprint role

    A Blueprint groups routes and code to keep the app organized and modular.
  2. Step 2: Compare options

    Only To organize routes and related code into reusable modules describes organizing routes and code. Others describe unrelated tasks.
  3. Final Answer:

    To organize routes and related code into reusable modules -> Option C
  4. Quick Check:

    Blueprint purpose = organize routes [OK]
Hint: Blueprints group routes for modular apps [OK]
Common Mistakes:
  • Thinking Blueprint creates database models
  • Confusing Blueprint with authentication
  • Assuming Blueprint serves static files
2. Which of the following is the correct way to create a Blueprint named 'blog' in Flask?
easy
A. blog = Blueprint('blog', app)
B. blog = Blueprint('blog')
C. blog = Blueprint(__name__, 'blog')
D. blog = Blueprint('blog', __name__)

Solution

  1. Step 1: Recall Blueprint constructor

    The Blueprint constructor requires the name and import name (usually __name__).
  2. Step 2: Check options

    blog = Blueprint('blog', __name__) uses correct order: name 'blog' and __name__. Others miss or swap arguments.
  3. Final Answer:

    blog = Blueprint('blog', __name__) -> Option D
  4. Quick Check:

    Blueprint(name, __name__) = correct [OK]
Hint: Blueprint needs name and __name__ args [OK]
Common Mistakes:
  • Omitting __name__ argument
  • Swapping argument order
  • Passing app instead of __name__
3. Given this code snippet, what will be the output when accessing the '/hello' route?
from flask import Flask, Blueprint
app = Flask(__name__)
hello_bp = Blueprint('hello', __name__)

@hello_bp.route('/hello')
def hello():
    return 'Hello from Blueprint!'

app.register_blueprint(hello_bp)

if __name__ == '__main__':
    app.run()
medium
A. 404 Not Found error
B. Hello from Blueprint!
C. 500 Internal Server Error
D. Empty response

Solution

  1. Step 1: Check route registration

    The route '/hello' is defined in the Blueprint and the Blueprint is registered on the app.
  2. Step 2: Understand response

    Accessing '/hello' calls the hello() function returning 'Hello from Blueprint!'.
  3. Final Answer:

    Hello from Blueprint! -> Option B
  4. Quick Check:

    Registered route returns defined string [OK]
Hint: Registered Blueprint routes respond normally [OK]
Common Mistakes:
  • Forgetting to register the Blueprint
  • Using wrong route path
  • Expecting error without registration
4. Identify the error in this Flask Blueprint setup:
from flask import Flask, Blueprint
app = Flask(__name__)
admin_bp = Blueprint('admin', __name__)

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

# Missing line here

if __name__ == '__main__':
    app.run()
medium
A. Blueprint is not registered with the app
B. Route decorator syntax is wrong
C. Blueprint is not created correctly
D. Function dashboard() is missing return statement

Solution

  1. Step 1: Check Blueprint creation and route

    Blueprint and route are correctly defined with proper syntax and return statement.
  2. Step 2: Look for registration

    The Blueprint is not registered with the Flask app using app.register_blueprint(admin_bp).
  3. Final Answer:

    Blueprint is not registered with the app -> Option A
  4. Quick Check:

    Missing app.register_blueprint() causes route not found [OK]
Hint: Always register Blueprint with app [OK]
Common Mistakes:
  • Forgetting app.register_blueprint()
  • Misplacing route decorator
  • Missing return in route function
5. You want to create two Blueprints, user_bp and admin_bp, each with a route '/profile'. How do you register them so that user_bp routes are under '/user' and admin_bp routes under '/admin' in the main app?
hard
A. app.register_blueprint(user_bp, url_prefix='/user')\napp.register_blueprint(admin_bp, url_prefix='/admin')
B. app.register_blueprint(user_bp)\napp.register_blueprint(admin_bp)
C. app.register_blueprint(user_bp, url_prefix='/admin')\napp.register_blueprint(admin_bp, url_prefix='/user')
D. user_bp.route('/user/profile')\nadmin_bp.route('/admin/profile')

Solution

  1. Step 1: Understand url_prefix usage

    To group routes under a path prefix, use url_prefix when registering Blueprints.
  2. Step 2: Apply prefixes correctly

    Register user_bp with url_prefix='/user' and admin_bp with url_prefix='/admin'.
  3. Final Answer:

    app.register_blueprint(user_bp, url_prefix='/user')\napp.register_blueprint(admin_bp, url_prefix='/admin') -> Option A
  4. Quick Check:

    url_prefix sets route base path [OK]
Hint: Use url_prefix to separate Blueprint routes [OK]
Common Mistakes:
  • Not using url_prefix causing route conflicts
  • Swapping prefixes between Blueprints
  • Trying to set route paths inside Blueprint incorrectly
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