A template filter in Flask is a function that modifies or formats data before displaying it in a template. It helps change how data looks without changing the data itself.
Click to reveal answer
beginner
How do you apply a built-in filter in a Flask template?
You apply a filter by using the pipe symbol (|) after a variable in the template. For example, {{ name|upper }} changes the text in 'name' to uppercase.
Click to reveal answer
intermediate
How can you create a custom template filter in Flask?
You create a custom filter by defining a Python function and then registering it with Flask using the @app.template_filter decorator or app.add_template_filter().
Click to reveal answer
intermediate
What is the purpose of using template filters instead of modifying data in the view?
Template filters keep the view code clean and separate data formatting from logic. This makes templates easier to read and maintain.
Click to reveal answer
beginner
Example: What does {{ 3.14159|round(2) }} output in a Flask template?
It outputs 3.14 by rounding the number 3.14159 to 2 decimal places using the built-in 'round' filter.
Click to reveal answer
Which symbol is used to apply a filter to a variable in a Flask template?
A|
B&
C#
D%
✗ Incorrect
The pipe symbol (|) is used to apply filters in Flask templates, like {{ variable|filter }}.
How do you register a custom filter in Flask?
AUsing @app.template_filter decorator
BUsing @app.route decorator
CUsing @app.before_request decorator
DUsing @app.errorhandler decorator
✗ Incorrect
Custom filters are registered with @app.template_filter or app.add_template_filter().
What does the built-in 'upper' filter do in Flask templates?
AConverts text to lowercase
BReverses the text
CConverts text to uppercase
DRemoves spaces from text
✗ Incorrect
The 'upper' filter changes all letters in a string to uppercase.
Why use template filters instead of formatting data in the Flask view?
AFilters slow down the app
BFilters change the original data permanently
CFilters are harder to maintain
DFilters keep templates and views separate and clean
✗ Incorrect
Filters help keep formatting in templates, making views simpler and code cleaner.
Which of these is NOT a built-in Flask template filter?
Acapitalize
Bpluralize
Clower
Dround
✗ Incorrect
'pluralize' is not a built-in filter in Flask; you would need to create a custom filter for that.
Explain how to create and use a custom template filter in Flask.
Think about how you connect Python code to templates.
You got /3 concepts.
Describe why template filters are useful in Flask templates.
Consider the difference between data and how it looks.
You got /4 concepts.
Practice
(1/5)
1. What does the | upper filter do in a Flask template?
easy
A. Truncates the text to a specified length
B. Rounds a number to the nearest integer
C. Formats a date to a readable string
D. Converts the text to uppercase letters
Solution
Step 1: Understand the purpose of the upper filter
The upper filter changes all letters in a string to uppercase.
Step 2: Compare with other filters
Other filters like round, truncate, and date formatting do different tasks unrelated to letter case.
Final Answer:
Converts the text to uppercase letters -> Option D
Quick Check:
upper = uppercase text [OK]
Hint: Remember: upper means all caps text [OK]
Common Mistakes:
Confusing upper with round or truncate
Thinking upper formats dates
Using upper on numbers expecting rounding
2. Which of the following is the correct syntax to apply the round filter to a variable price in a Flask template?
easy
A. {{ price | round(2) }}
B. {{ round(price, 2) }}
C. {{ price.round(2) }}
D. {{ price | round:2 }}
Solution
Step 1: Recall Flask template filter syntax
Filters are applied using the pipe symbol | followed by the filter name and optional arguments in parentheses.
Step 2: Identify the correct syntax for round
The correct syntax is {{ price | round(2) }}. Other options either misuse method call syntax or wrong separator.
Final Answer:
{{ price | round(2) }} -> Option A
Quick Check:
Filter syntax uses pipe and parentheses [OK]
Hint: Use pipe (|) then filter name with parentheses [OK]