Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to register a custom template filter named 'cut'.
Django
from django import template register = template.[1]() @register.filter def cut(value, arg): return value.replace(arg, '')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Filter()' instead of 'Library()' to create the register object.
Forgetting to create the register object before defining filters.
✗ Incorrect
You create a template Library instance with template.Library() to register custom filters.
2fill in blank
mediumComplete the code to apply the custom filter 'cut' in a Django template.
Django
{{ '{{ some_text|[1]:"a" }}' }} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect filter names like 'remove' or 'replace' which are not registered.
Forgetting to use quotes around the argument in the template.
✗ Incorrect
You use the filter name 'cut' in the template with the pipe symbol to apply it.
3fill in blank
hardFix the error in the filter function to handle None values safely.
Django
@register.filter def cut(value, arg): if value is None: return [1] return value.replace(arg, '')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None causes template rendering errors.
Returning the argument instead of a string.
✗ Incorrect
Returning an empty string '' when value is None avoids errors in templates.
4fill in blank
hardFill both blanks to register a filter that converts text to uppercase.
Django
@register.filter(name=[1]) def [2](value): return value.upper()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted filter names causes syntax errors.
Using quotes around the function name.
✗ Incorrect
The filter name is a string 'uppercase' and the function name is 'upper' to convert text.
5fill in blank
hardFill all three blanks to create a filter that repeats text n times.
Django
@register.filter def [1](value, n): return value * [2] # Usage in template: {{ '{{ some_text|repeat:[3] }}' }}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names that don't match the filter usage.
Multiplying by value instead of n.
Passing variable names instead of a number in the template.
✗ Incorrect
The function name is 'repeat', it multiplies value by n, and in template n can be 3.