Challenge - 5 Problems
Flash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Flask flash message display behave?
Consider a Flask app that flashes a message and redirects to a page that displays it. What will the user see after the redirect?
Flask
from flask import Flask, flash, redirect, url_for, render_template_string, get_flashed_messages app = Flask(__name__) app.secret_key = 'secret' @app.route('/set') def set_message(): flash('Welcome back!') return redirect(url_for('show_message')) @app.route('/show') def show_message(): messages = get_flashed_messages() return render_template_string('<ul>{% for msg in messages %}<li>{{ msg }}</li>{% endfor %}</ul>', messages=messages)
Attempts:
2 left
💡 Hint
Remember that flash messages are stored in the session and shown once after redirect.
✗ Incorrect
Flask flash messages are stored in the session and retrieved by get_flashed_messages(). After redirect, the message is shown once and then removed. So the user sees the message exactly once.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in flashing a message
Which option contains a syntax error when trying to flash a message in Flask?
Flask
from flask import flash # Flash a message example flash('Error occurred', category='error')
Attempts:
2 left
💡 Hint
Check if the category argument is a string or a variable.
✗ Incorrect
Option D uses category=error without quotes, so Python treats error as a variable which is not defined, causing a NameError.
❓ state_output
advanced2:00remaining
What is the output after multiple flash calls before rendering?
If you flash multiple messages before rendering a template that shows them all, what will be displayed?
Flask
from flask import Flask, flash, render_template_string, get_flashed_messages app = Flask(__name__) app.secret_key = 'secret' @app.route('/') def index(): flash('First message') flash('Second message', 'info') messages = get_flashed_messages(with_categories=True) return render_template_string('''<ul>{% for category, msg in messages %}<li>{{ category }}: {{ msg }}</li>{% endfor %}</ul>''', messages=messages)
Attempts:
2 left
💡 Hint
Default category is 'message' if not specified.
✗ Incorrect
The first flash uses default category 'message'. The second uses 'info'. Using with_categories=True returns tuples with category and message.
🔧 Debug
advanced2:00remaining
Why does the flash message not appear after redirect?
A developer flashes a message and redirects, but the message never shows. What is the most likely cause?
Flask
from flask import Flask, flash, redirect, url_for app = Flask(__name__) @app.route('/login') def login(): flash('Logged in successfully') return redirect(url_for('dashboard')) @app.route('/dashboard') def dashboard(): return 'Dashboard page'
Attempts:
2 left
💡 Hint
Flashing uses session, which needs a secret key.
✗ Incorrect
Without setting app.secret_key, Flask cannot store session data, so flash messages are lost after redirect.
🧠 Conceptual
expert3:00remaining
How does Flask flash message storage and retrieval work internally?
Which statement best describes how Flask stores and retrieves flash messages for user feedback?
Attempts:
2 left
💡 Hint
Think about how Flask keeps data between requests per user.
✗ Incorrect
Flask stores flash messages in the session, which is user-specific and stored client-side in a secure cookie. Messages are removed after retrieval to show only once.