0
0
Flaskframework~20 mins

Flash messages for user feedback in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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)
AThe page at /show will display nothing because flash messages are cleared immediately.
BThe page at /show will display a list with one item: 'Welcome back!'
CThe page at /show will show an error because get_flashed_messages() requires arguments.
DThe page at /show will display the message multiple times if refreshed.
Attempts:
2 left
💡 Hint
Remember that flash messages are stored in the session and shown once after redirect.
📝 Syntax
intermediate
2: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')
Aflash('Error occurred', category='error')
Bflash('Error occurred', 'error')
Cflash(message='Error occurred', category='error')
Dflash('Error occurred', category=error)
Attempts:
2 left
💡 Hint
Check if the category argument is a string or a variable.
state_output
advanced
2: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)
A<ul><li>message: First message</li><li>info: Second message</li></ul>
B<ul><li>First message</li><li>Second message</li></ul>
C<ul><li>error: First message</li><li>info: Second message</li></ul>
D<ul><li>default: First message</li><li>info: Second message</li></ul>
Attempts:
2 left
💡 Hint
Default category is 'message' if not specified.
🔧 Debug
advanced
2: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'
AThe flash message is not displayed because redirect clears it.
BThe flash message requires a template to be rendered, not a string.
CThe app.secret_key is missing, so session data (flash) is not saved.
DThe flash function must be called after redirect, not before.
Attempts:
2 left
💡 Hint
Flashing uses session, which needs a secret key.
🧠 Conceptual
expert
3: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?
AFlash messages are stored in the user's session cookie and removed after being retrieved once.
BFlash messages are stored in a global server variable and shared across all users until cleared.
CFlash messages are saved in a database and fetched on each page load until expired.
DFlash messages are embedded in the URL query parameters and parsed on the next request.
Attempts:
2 left
💡 Hint
Think about how Flask keeps data between requests per user.