0
0
Djangoframework~10 mins

Custom template filters in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
ALibrary
BFilter
CTag
DRegister
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.
2fill in blank
medium

Complete 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'
Aremove
Breplace
Ccut
Ddelete
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.
3fill in blank
hard

Fix 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'
A''
BNone
Cvalue
Darg
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None causes template rendering errors.
Returning the argument instead of a string.
4fill in blank
hard

Fill 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'
A"uppercase"
Buppercase
C"upper"
Dupper
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted filter names causes syntax errors.
Using quotes around the function name.
5fill in blank
hard

Fill 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'
Arepeat
Bn
C3
Dvalue
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.