Challenge - 5 Problems
Template Filters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the output of this Django template snippet?
Given the context variable
my_date with value 2024-06-15, what will this template output?{{ my_date|date:"Y-m-d" }}Attempts:
2 left
💡 Hint
The date filter formats the date according to the given string pattern.
✗ Incorrect
The filter uses the format string 'Y-m-d' which means 4-digit year, dash, 2-digit month, dash, 2-digit day.
❓ state_output
intermediate1:30remaining
What is the length output of this list in a Django template?
Given the context variable
items as ["apple", "banana", "cherry"], what does this template output?{{ items|length }}Attempts:
2 left
💡 Hint
The length filter counts the number of items in a list.
✗ Incorrect
The list has three elements, so length returns 3.
📝 Syntax
advanced2:00remaining
Which template filter usage correctly provides a default value?
You want to display the variable
username but show 'Guest' if it is empty or undefined. Which option does this correctly?Attempts:
2 left
💡 Hint
The default filter replaces empty or undefined variables with the given string.
✗ Incorrect
The default filter takes a string argument in quotes. default_if_none only replaces None, not empty strings.
🔧 Debug
advanced2:00remaining
Why does this template raise an error?
Template snippet:
Assuming
{{ my_date|date:Y-m-d }}Assuming
my_date is a valid date object, why does this cause an error?Attempts:
2 left
💡 Hint
Check how the date format string is passed to the filter.
✗ Incorrect
The date filter requires the format string to be quoted. Without quotes, the template parser throws an error.
🧠 Conceptual
expert2:30remaining
What is the output of this Django template code?
Given the context variable
user_name with value None, what does this template output?{{ user_name|default:"Anonymous"|length }}Attempts:
2 left
💡 Hint
The default filter replaces None with 'Anonymous', then length counts characters.
✗ Incorrect
user_name is None, so default replaces it with 'Anonymous'. The length filter then counts the characters in 'Anonymous', which is 9.