0
0
Flaskframework~20 mins

Blueprint routes and templates in Flask - Practice Problems & Coding Challenges

Choose your learning style9 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 a Blueprint route?
Given this Flask Blueprint setup, what will the browser display when visiting /blog/?
Flask
from flask import Blueprint, render_template_string

blog = Blueprint('blog', __name__, template_folder='templates')

@blog.route('/')
def index():
    return render_template_string('<h1>Blog Home</h1>')

# Assume app.register_blueprint(blog, url_prefix='/blog') is done
A404 Not Found error
B<h1>Blog Home</h1>
C500 Internal Server Error
DEmpty page with status 200
Attempts:
2 left
💡 Hint
Think about how Blueprint routes are registered with a URL prefix.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in Blueprint route definition
Which option contains a syntax error in defining a Flask Blueprint route?
Flask
from flask import Blueprint

api = Blueprint('api', __name__)

@api.route('/data')
def get_data():
    return 'Data response'
A
@api.route('/data')
def get_data()
    return 'Data response'
B
@api.route('/data')
def get_data():
    return 'Data response'
C
'esnopser ataD' nruter    
:)(atad_teg fed
)'atad/'(etuor.ipa@
D
api.route('/data')
def get_data():
    return 'Data response'
Attempts:
2 left
💡 Hint
Check for missing colons and indentation.
state_output
advanced
2:00remaining
What template is rendered by this Blueprint route?
Given this Blueprint and template setup, what will be rendered when visiting /shop/?
Flask
from flask import Blueprint, render_template

shop = Blueprint('shop', __name__, template_folder='shop_templates')

@shop.route('/')
def home():
    return render_template('home.html')

# Assume app.register_blueprint(shop, url_prefix='/shop')

# Content of shop_templates/home.html:
# <h2>Welcome to the Shop</h2>
A<h2>Welcome to the Shop</h2>
B<h1>Shop Home</h1>
CTemplateNotFound error
DEmpty response with status 200
Attempts:
2 left
💡 Hint
Check the template folder specified in the Blueprint and the template file name.
🔧 Debug
advanced
2:00remaining
Why does this Blueprint route cause a 404 error?
Consider this Blueprint code and registration. Why does visiting /admin/dashboard return 404?
Flask
from flask import Blueprint

admin = Blueprint('admin', __name__)

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

# In main app:
# app.register_blueprint(admin, url_prefix='/admin/')
AThe function 'dashboard' is missing a return statement.
BThe route '/dashboard' inside Blueprint is incorrect syntax.
CThe url_prefix has a trailing slash causing route mismatch.
DBlueprint name 'admin' conflicts with Flask reserved keywords.
Attempts:
2 left
💡 Hint
Check how url_prefix with trailing slash affects route matching.
🧠 Conceptual
expert
3:00remaining
How does Flask resolve templates in nested Blueprints?
If you have a Blueprint 'store' with template_folder='templates/store' and inside it a nested Blueprint 'products' with template_folder='templates/store/products', which template path will Flask look for when rendering render_template('list.html') inside 'products' Blueprint?
Atemplates/list.html
Btemplates/store/products/templates/list.html
Ctemplates/store/list.html
Dtemplates/store/products/list.html
Attempts:
2 left
💡 Hint
Blueprints use their own template_folder as base for render_template calls inside them.