The code flashes a message 'Welcome back!' with category 'info'. Using get_flashed_messages(with_categories=True) returns a list of (category, message) tuples.
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>.
Final Answer:
<div class="info">Welcome back!</div> -> Option D
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?
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
Step 1: Recall flash message requirements
Flashing messages requires session support, which needs app.secret_key to be set.
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.
Final Answer:
Missing app.secret_key causes flash to fail -> Option B
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
Step 1: Flash messages with categories
Use flash('message', 'category') -- category as the second positional argument.
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.
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.
Final Answer:
flash('Form submitted!', 'success'); flash('Invalid input', 'error'); template with get_flashed_messages(with_categories=True) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.