0
0
Flaskframework~20 mins

Flash message categories in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flash Message Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output category of this flash message?
Given the Flask code below, what category will the flash message have when rendered?
Flask
from flask import Flask, flash, render_template_string
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/')
def index():
    flash('Welcome back!', 'info')
    return render_template_string('''
    {% with messages = get_flashed_messages(with_categories=true) %}
      {% for category, message in messages %}
        <div class="alert alert-{{ category }}">{{ message }}</div>
      {% endfor %}
    {% endwith %}
    ''')
Ainfo
Bmessage
Calert
Dflash
Attempts:
2 left
💡 Hint
Look at the second argument passed to the flash function.
state_output
intermediate
2:00remaining
How many flash messages are stored after these calls?
After running the following Flask code, how many flash messages are stored in the session?
Flask
flash('Error occurred', 'error')
flash('Please login', 'warning')
flash('Welcome!', 'success')
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Each flash call adds one message to the session.
📝 Syntax
advanced
2:00remaining
Which option causes a NameError in flash message category usage?
Identify the option that will cause a NameError when trying to flash a message with a category.
Flask
flash('Update successful', info)
Aflash('Update successful', info)
Bflash('Update successful', category='info')
Cflash(message='Update successful', category='info')
Dflash('Update successful', 'info')
Attempts:
2 left
💡 Hint
Check if the category argument is a string or a variable.
🔧 Debug
advanced
2:00remaining
Why does this flash message category not appear in the template?
Given this Flask code, why does the flash message with category 'notice' not show in the template?
Flask
flash('Check your email', 'notice')

# Template snippet:
# {% with messages = get_flashed_messages(with_categories=true) %}
#   {% for category, message in messages %}
#     {% if category in ['info', 'warning', 'error', 'success'] %}
#       <div class="alert alert-{{ category }}">{{ message }}</div>
#     {% endif %}
#   {% endfor %}
# {% endwith %}
AFlash messages with category 'notice' are not stored in the session.
BThe template filters out categories not in the list, so 'notice' is ignored.
CThe flash function does not accept 'notice' as a valid category.
DThe secret key is missing, so flash messages are not saved.
Attempts:
2 left
💡 Hint
Check the if condition in the template loop.
🧠 Conceptual
expert
2:00remaining
What is the purpose of flash message categories in Flask?
Choose the best explanation for why Flask uses categories with flash messages.
ATo automatically log messages to the server console based on category.
BTo restrict flash messages to only predefined types enforced by Flask.
CTo encrypt flash messages for security during transmission.
DTo allow different types of messages to be styled or handled differently in templates.
Attempts:
2 left
💡 Hint
Think about how categories help in user interface design.