0
0
Flaskframework~10 mins

Error message display in templates in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to display an error message in a Flask template using Jinja2 syntax.

Flask
<p>{{ [1] }}</p>
Drag options to blanks, or click blank then click option'
Aerror_message
Bmessage
Cerror
Dflash
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not passed from the Flask view.
Trying to call a function inside the template instead of displaying a variable.
2fill in blank
medium

Complete the Flask view code to pass an error message to the template named 'form.html'.

Flask
from flask import render_template

@app.route('/submit', methods=['POST'])
def submit():
    error = 'Invalid input'
    return render_template('form.html', [1]=error)
Drag options to blanks, or click blank then click option'
Aerror
Bmessage
Cflash
Dmsg
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a different key name than the one used in the template.
Forgetting to pass the error variable to the template.
3fill in blank
hard

Fix the error in the template code to correctly display the error message only if it exists.

Flask
{% if [1] %}
  <div class="error">{{ error }}</div>
{% endif %}
Drag options to blanks, or click blank then click option'
Aerror_message
Berror
Cmessage
Dflash
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined in the template context.
Using incorrect Jinja2 syntax for the if statement.
4fill in blank
hard

Fill both blanks to create a Flask template snippet that loops over multiple error messages stored in a list called 'errors'.

Flask
{% for [1] in [2] %}
  <p class="error">{{ [1] }}</p>
{% endfor %}
Drag options to blanks, or click blank then click option'
Aerror
Berrors
Cmsg
Dmessages
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both loop variable and list.
Using a variable name not passed from the Flask view.
5fill in blank
hard

Fill all three blanks to create a Flask view function that flashes an error message and redirects to the 'index' page.

Flask
from flask import flash, redirect, url_for

@app.route('/login', methods=['POST'])
def login():
    flash([1], category=[2])
    return redirect(url_for([3]))
Drag options to blanks, or click blank then click option'
A'Login failed'
B'error'
C'index'
D'success'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string literals.
Using wrong category names or route names.