Complete the code to display an error message in a Flask template using Jinja2 syntax.
<p>{{ [1] }}</p>The variable error is commonly used to pass error messages to the template in Flask. Using {{ error }} displays the error message.
Complete the Flask view code to pass an error message to the template named 'form.html'.
from flask import render_template @app.route('/submit', methods=['POST']) def submit(): error = 'Invalid input' return render_template('form.html', [1]=error)
The key error is used to pass the error message variable error to the template. This allows the template to access it as {{ error }}.
Fix the error in the template code to correctly display the error message only if it exists.
{% if [1] %}
<div class="error">{{ error }}</div>
{% endif %}The condition should check if the variable error exists and is truthy before displaying it. Using {% if error %} is correct.
Fill both blanks to create a Flask template snippet that loops over multiple error messages stored in a list called 'errors'.
{% for [1] in [2] %}
<p class="error">{{ [1] }}</p>
{% endfor %}The loop variable is error and the list is errors. This loops over each error message to display it.
Fill all three blanks to create a Flask view function that flashes an error message and redirects to the 'index' page.
from flask import flash, redirect, url_for @app.route('/login', methods=['POST']) def login(): flash([1], category=[2]) return redirect(url_for([3]))
The flash function sends the message 'Login failed' with category 'error'. The redirect goes to the index page.