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?
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)
Think about how the {% if %} statement works with empty strings in Jinja2 templates.
In Jinja2, empty strings are treated as false in conditionals. So, if error is empty, the {% else %} block runs, showing 'No errors'.
Which option contains a syntax error in the Jinja2 template code for displaying an error message?
{% if error %}Error: {{ error }}{% endif else %}No errors{% endif %}Look carefully at the placement of {% endif %} and {% else %} tags.
Option A incorrectly places {% endif %} before {% else %}, which breaks the if-else block syntax in Jinja2.
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)Think about how Jinja2 loops over lists in templates.
The template loops over each error string and creates a list item for each. So both errors appear as separate <li> elements.
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)Consider how Jinja2 renders None values.
Jinja2 converts None to an empty string when rendering, so the paragraph shows 'Error: ' with no visible message.
Choose the template snippet that best supports accessibility when showing error messages in a Flask app.
Think about how screen readers detect important messages.
Using role="alert" and aria-live="assertive" tells assistive technologies to announce the error immediately, improving accessibility.