Recall & Review
beginner
What does the
date filter do in Django templates?The
date filter formats a date or datetime object into a string according to the given format. For example, {{ my_date|date:"F j, Y" }} will show the date as 'January 1, 2024'.Click to reveal answer
beginner
How do you find the number of items in a list or characters in a string using Django template filters?
Use the
length filter. For example, {{ my_list|length }} shows how many items are in the list, and {{ my_string|length }} shows how many characters are in the string.Click to reveal answer
beginner
What is the purpose of the
default filter in Django templates?The
default filter provides a fallback value if the variable is empty or not defined. For example, {{ username|default:"Guest" }} will show 'Guest' if username is empty.Click to reveal answer
intermediate
How would you format a date to show only the year using the
date filter?Use the format string 'Y' with the
date filter. For example, {{ my_date|date:"Y" }} will display only the year like '2024'.Click to reveal answer
beginner
What happens if you use the
default filter on a variable that has a value?If the variable has a value (not empty or None), the
default filter returns that value unchanged. It only uses the fallback if the variable is empty or missing.Click to reveal answer
Which filter would you use to show the number of characters in a string in a Django template?
✗ Incorrect
The
length filter returns the number of characters in a string or items in a list.What does {{ my_date|date:"F j, Y" }} output if my_date is January 1, 2024?
✗ Incorrect
The format string 'F j, Y' outputs the full month name, day without leading zero, and full year.
If a variable is empty, which filter helps you show a fallback text?
✗ Incorrect
The
default filter shows a fallback value if the variable is empty or missing.What will {{ my_list|length }} show if my_list = [1, 2, 3, 4]?
✗ Incorrect
The
length filter returns the number of items in the list, which is 4.Which filter formats a datetime object in Django templates?
✗ Incorrect
The
date filter formats date or datetime objects into readable strings.Explain how to use the
date, length, and default filters in Django templates with examples.Think about how you would show a formatted date, count items, or show a default text if a variable is empty.
You got /3 concepts.
Describe a real-life scenario where you might use the
default filter in a Django template.Imagine a website greeting a user who might not be logged in.
You got /3 concepts.