5. You want to display multiple error messages in a Flask template, but only if they exist. Which template code correctly shows all errors from a list errors with accessible markup?
hard
A.
{% if errors %}
{% for e in errors %}
{{ e }}
{% endfor %}
{% endif %}
B.
{% for e in errors %}
{{ e }}
{% endfor %}
C.
{% if errors %}
{{ errors }}
{% endif %}
D.
{{ errors }}
Solution
Step 1: Check for errors existence before display
Using {% if errors %} ensures the list is not empty before rendering.
Step 2: Loop through errors with {% for %} and use accessible markup
Each error is shown in a list item inside a <ul> with aria-live="polite" for screen readers.
Final Answer:
{% if errors %} with {% for %} loop in accessible <ul aria-live="polite"> -> Option A
Quick Check:
Check if errors + loop + aria-live = A [OK]
Hint: Use {% if %} + {% for %} + aria-live for accessible error lists [OK]
Common Mistakes:
Not checking if errors exist before looping
Displaying the whole list as a string
Missing accessibility attributes for screen readers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.