0
0
Djangoframework~20 mins

Custom template filters in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this custom filter usage?

Given this custom filter that reverses a string, what will be the output of {{ 'hello'|reverse_string }} in a Django template?

Django
from django import template
register = template.Library()

@register.filter
def reverse_string(value):
    return value[::-1]
Aolleh
Bh e l l o
CError: filter not found
Dhello
Attempts:
2 left
💡 Hint

Think about what slicing with [::-1] does to a string.

📝 Syntax
intermediate
2:00remaining
Which option correctly registers a custom filter?

Which of the following code snippets correctly registers a custom Django template filter named double that doubles a number?

A
from django import template
register = template.Library()

@register.filter(name='double')
def double(value):
    return value * 2
B
from django.template import Library
register = Library()

def double(value):
    return value * 2
register.filter(double)
C
import django.template
register = django.template.Library()

@register.filter
def double(value):
    return value + value
D
from django import template
register = template.Library()

def double(value):
    return value * 2
register.filter('double', double)
Attempts:
2 left
💡 Hint

Look for the correct decorator usage and import style.

🔧 Debug
advanced
2:00remaining
Why does this custom filter cause a TemplateSyntaxError?

Consider this filter code:

from django import template
register = template.Library()

@register.filter
def add_prefix(value, prefix):
    return prefix + value

Using {{ 'world'|add_prefix:'Hello ' }} in a template causes a TemplateSyntaxError. Why?

AThe filter function must accept exactly one argument, but this one has two.
BThe filter is missing the <code>is_safe=True</code> parameter in the decorator.
CThe filter function must accept the value and an optional argument, but the template syntax is incorrect.
DThe filter function must be registered with <code>@register.simple_tag</code> instead.
Attempts:
2 left
💡 Hint

Check how to pass arguments to filters in Django templates.

state_output
advanced
2:00remaining
What is the output of this chained custom filter usage?

Given these filters:

from django import template
register = template.Library()

@register.filter
def shout(value):
    return value.upper() + '!!!'

@register.filter
def repeat(value, times=2):
    return value * times

What is the output of {{ 'hi'|shout|repeat:3 }} in a Django template?

Ahi!!!hi!!!hi!!!
BHI!!!HI!!!HI!!!
CHIHIHI!!!
DError: repeat filter requires integer argument
Attempts:
2 left
💡 Hint

Remember the order filters are applied and how arguments are passed.

🧠 Conceptual
expert
2:00remaining
Which statement about custom template filters is TRUE?

Choose the correct statement about Django custom template filters.

AFilters can accept multiple arguments by defining the function with *args and **kwargs.
BFilters can modify the template context directly to affect other variables.
CCustom filters must always return strings; returning other types causes errors.
DFilters must be registered with a Library instance and can be used in templates after loading the tag library.
Attempts:
2 left
💡 Hint

Think about how filters are registered and used in templates.