Challenge - 5 Problems
Flash Message Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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 %} ''')
Attempts:
2 left
💡 Hint
Look at the second argument passed to the flash function.
✗ Incorrect
The second argument to flash sets the category. Here it is 'info', so the message category is 'info'.
❓ state_output
intermediate2: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')
Attempts:
2 left
💡 Hint
Each flash call adds one message to the session.
✗ Incorrect
Each call to flash adds one message with its category. Three calls mean three messages stored.
📝 Syntax
advanced2: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)Attempts:
2 left
💡 Hint
Check if the category argument is a string or a variable.
✗ Incorrect
Option A uses info without quotes, which is undefined and causes a NameError. A, B, and D are valid.
🔧 Debug
advanced2: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 %}
Attempts:
2 left
💡 Hint
Check the if condition in the template loop.
✗ Incorrect
The template only renders messages with categories in ['info', 'warning', 'error', 'success']. 'notice' is not included, so it is skipped.
🧠 Conceptual
expert2:00remaining
What is the purpose of flash message categories in Flask?
Choose the best explanation for why Flask uses categories with flash messages.
Attempts:
2 left
💡 Hint
Think about how categories help in user interface design.
✗ Incorrect
Categories let developers assign types like 'error' or 'success' so templates can style or display messages differently.