0
0
Flaskframework~20 mins

Error message display in templates in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Error Display Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Flask display error messages in templates?

Consider a Flask app that passes an error message to a template using render_template. What will the template show if the error message is empty?

Flask
from flask import Flask, render_template_string
app = Flask(__name__)

@app.route('/')
def home():
    error = ''
    return render_template_string('<p>{% if error %}Error: {{ error }}{% else %}No errors{% endif %}</p>', error=error)
AThe page shows: 'Error: ' with no message
BThe page raises a TemplateSyntaxError
CThe page shows nothing
DThe page shows: 'No errors'
Attempts:
2 left
💡 Hint

Think about how the {% if %} statement works with empty strings in Jinja2 templates.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Flask template error display

Which option contains a syntax error in the Jinja2 template code for displaying an error message?

Flask
{% if error %}Error: {{ error }}{% endif else %}No errors{% endif %}
A{% if error %}Error: {{ error }}{% endif else %}No errors{% endif %}
B}% fidne %{srorre oN}% esle %{}} rorre {{ :rorrE}% rorre fi %{
C{% if error %}Error: {{ error }}{% else %}No errors{% endif %}
D% if error %}Error: {{ error }}{% else %}No errors{% endif %}
Attempts:
2 left
💡 Hint

Look carefully at the placement of {% endif %} and {% else %} tags.

state_output
advanced
2:00remaining
What is the output when multiple error messages are passed as a list?

Given this Flask route and template snippet, what will be the rendered output?

from flask import Flask, render_template_string
app = Flask(__name__)

@app.route('/')
def home():
    errors = ['Name required', 'Email invalid']
    return render_template_string('''
<ul>
{% for error in errors %}
  <li>{{ error }}</li>
{% endfor %}
</ul>
''', errors=errors)
A<ul></ul>
B<ul><li>Name required, Email invalid</li></ul>
C<ul><li>Name required</li><li>Email invalid</li></ul>
DTemplate rendering error due to list
Attempts:
2 left
💡 Hint

Think about how Jinja2 loops over lists in templates.

🔧 Debug
advanced
2:00remaining
Why does this Flask template not show the error message?

Given this Flask route and template, why does the error message not appear on the page?

from flask import Flask, render_template_string
app = Flask(__name__)

@app.route('/')
def home():
    error = None
    return render_template_string('<p>Error: {{ error }}</p>', error=error)
ABecause error is None, it renders as an empty string, so no visible message
BBecause the variable 'error' is not passed to the template
CBecause Flask does not allow None values in templates
DBecause the template syntax is invalid and raises an error
Attempts:
2 left
💡 Hint

Consider how Jinja2 renders None values.

🧠 Conceptual
expert
3:00remaining
Which option correctly implements error message display with accessibility in Flask templates?

Choose the template snippet that best supports accessibility when showing error messages in a Flask app.

A<p>{{ error }}</p>
B<div role="alert" aria-live="assertive">{{ error }}</div>
C<span style="color:red">{{ error }}</span>
D<div aria-hidden="true">{{ error }}</div>
Attempts:
2 left
💡 Hint

Think about how screen readers detect important messages.