📖 Scenario: You are building a simple Flask web app that shows a list of product prices. You want to display the prices formatted as currency in the HTML page.
🎯 Goal: Create a Flask app with a custom template filter called currency that formats numbers as prices with a dollar sign and two decimals. Use this filter in the HTML template to show the product prices nicely.
📋 What You'll Learn
Create a Flask app with a dictionary called products containing product names and prices.
Add a custom template filter named currency that formats a number as a string with a dollar sign and two decimal places.
Use the currency filter in the Jinja2 template to display each product's price.
Render the template with the products dictionary passed in.
💡 Why This Matters
🌍 Real World
Formatting prices nicely on web pages is common in online stores and dashboards.
💼 Career
Knowing how to create and use custom template filters in Flask helps build clean, reusable UI components.
Progress0 / 4 steps
1
Create the products dictionary
Create a dictionary called products with these exact entries: 'Apple': 0.5, 'Banana': 0.3, 'Cherry': 0.2.
Flask
Hint
Use curly braces to create a dictionary with the exact keys and values.
2
Add the currency template filter
Create a Flask app instance called app. Then add a template filter named currency using the @app.template_filter('currency') decorator. The filter function should take a number and return a string formatted as a dollar sign followed by the number with two decimals (e.g., '$0.50').
Flask
Hint
Use @app.template_filter('currency') above your function to register the filter.
3
Create the HTML template using the currency filter
Create a Jinja2 template string called template that loops over products.items() with variables name and price. Inside the loop, display the product name and the price filtered with |currency. Use an unordered list <ul> and list items <li>.
Flask
Hint
Use Jinja2 syntax with {% for %} and {{ }} to loop and display values.
4
Render the template with products in a route
Add a route @app.route('/') with a function index() that returns app.jinja_env.from_string(template).render(products=products) to render the template with the products dictionary.
Flask
Hint
Use @app.route('/') to create the home page and render the template string with products.
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]