Bird
Raised Fist0
Flaskframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using flash() in a Flask application?
easy
A. To create a new route in the Flask app
B. To permanently store user data in the session
C. To send a one-time message to the user after a request
D. To handle database connections automatically

Solution

  1. Step 1: Understand the role of flash messages

    Flash messages are temporary messages shown to users after actions like form submission.
  2. Step 2: Identify what flash() does

    The flash() function stores these messages to be displayed once, then removed.
  3. Final Answer:

    To send a one-time message to the user after a request -> Option C
  4. Quick Check:

    Flash messages = one-time user feedback [OK]
Hint: Flash messages show temporary feedback after user actions [OK]
Common Mistakes:
  • Thinking flash stores data permanently
  • Confusing flash with routing or database functions
  • Assuming flash messages appear multiple times
2. Which of the following is the correct way to import and use flash in a Flask app?
easy
A. from flask import flash; flash('Message')
B. import flash from flask; flash('Message')
C. from flask.flash import flash; flash('Message')
D. import flask.flash; flask.flash('Message')

Solution

  1. Step 1: Recall correct import syntax in Flask

    Functions like flash are imported directly from the flask package using from flask import flash.
  2. Step 2: Check usage after import

    After importing, calling flash('Message') is the correct way to flash a message.
  3. Final Answer:

    from flask import flash; flash('Message') -> Option A
  4. Quick Check:

    Correct import and call = from flask import flash; flash('Message') [OK]
Hint: Use 'from flask import flash' to import flash [OK]
Common Mistakes:
  • Using incorrect import syntax
  • Trying to import flash as a submodule
  • Calling flash without importing
3. Given this Flask code snippet, what will be the output on the rendered page?
from flask import Flask, flash, render_template_string, get_flashed_messages
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/')
def index():
    flash('Welcome back!', 'info')
    messages = get_flashed_messages(with_categories=True)
    return render_template_string('''
        {% for category, message in messages %}
            <div class="{{ category }}">{{ message }}</div>
        {% endfor %}
    ''', messages=messages)
medium
A. No output because flash messages are empty
B.
Welcome back!
C. SyntaxError due to incorrect template syntax
D.
Welcome back!

Solution

  1. Step 1: Understand flash message with category

    The code flashes a message 'Welcome back!' with category 'info'. Using get_flashed_messages(with_categories=True) returns a list of (category, message) tuples.
  2. Step 2: Analyze template rendering

    The template loops over messages and creates a div with class set to the category and content as the message. So it outputs <div class="info">Welcome back!</div>.
  3. Final Answer:

    <div class="info">Welcome back!</div> -> Option D
  4. Quick Check:

    Flash with category 'info' outputs div with class 'info' [OK]
Hint: Flash with categories outputs div with category as class [OK]
Common Mistakes:
  • Ignoring categories and using wrong class name
  • Assuming flash messages are empty immediately
  • Misreading template syntax causing syntax error
4. What is wrong with this Flask code that tries to flash a message?
from flask import Flask, flash
app = Flask(__name__)

@app.route('/')
def index():
    flash('Hello user!')
    return 'Done'
medium
A. Flash messages must be returned, not just called
B. Missing app.secret_key causes flash to fail
C. Flash requires a category argument
D. Flash cannot be used inside route functions

Solution

  1. Step 1: Recall flash message requirements

    Flashing messages requires session support, which needs app.secret_key to be set.
  2. Step 2: Identify missing secret key

    The code does not set app.secret_key, so flashing will not work properly and may raise an error.
  3. Final Answer:

    Missing app.secret_key causes flash to fail -> Option B
  4. Quick Check:

    Secret key needed for flash session [OK]
Hint: Always set app.secret_key before using flash [OK]
Common Mistakes:
  • Forgetting to set secret_key
  • Thinking flash needs category argument
  • Believing flash must be returned
5. You want to show different flash messages with categories 'success' and 'error' after form submission. Which code snippet correctly flashes both messages and displays them in a template with distinct styles?
hard
A. flash('Form submitted!', 'success')\nflash('Invalid input', 'error')\nreturn render_template('index.html')\n \n{% for category, msg in get_flashed_messages(with_categories=True) %}\n

{{ msg }}

\n{% endfor %}
B. flash('Form submitted!')\nflash('Invalid input')\nreturn render_template('index.html')\n \n{% for msg in get_flashed_messages() %}\n

{{ msg }}

\n{% endfor %}
C. flash('Form submitted!', type='success')\nflash('Invalid input', type='error')\nreturn render_template('index.html')\n \n{% for category, msg in get_flashed_messages(with_categories=True) %}\n

{{ msg }}

\n{% endfor %}
D. flash('Form submitted!', 'success')\nflash('Invalid input', 'error')\nreturn render_template('index.html', messages=get_flashed_messages())\n \n{% for category, msg in messages %}\n

{{ msg }}

\n{% endfor %}

Solution

  1. Step 1: Flash messages with categories

    Use flash('message', 'category') -- category as the second positional argument.
  2. Step 2: Retrieve messages with categories in template

    Loop over get_flashed_messages(with_categories=True) directly in the template to unpack (category, message) and apply class="{{ category }}" for distinct styles.
  3. Step 3: Distractors fail because

    No categories provided; invalid keyword argument like type='success' raises TypeError; retrieving without with_categories=True returns strings causing unpack error in for category, msg in messages.
  4. Final Answer:

    flash('Form submitted!', 'success'); flash('Invalid input', 'error'); template with get_flashed_messages(with_categories=True) -> Option A
  5. Quick Check:

    Flash with categories + template loop with categories = flash('Form submitted!', 'success')\nflash('Invalid input', 'error')\nreturn render_template('index.html')\n \n{% for category, msg in get_flashed_messages(with_categories=True) %}\n

    {{ msg }}

    \n{% endfor %} [OK]
Hint: Flash with category, get_flashed_messages(with_categories=True) in template [OK]
Common Mistakes:
  • Not passing with_categories=True when retrieving messages
  • Using invalid keyword argument like type= for category
  • Passing messages without categories to template expecting categories
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard