Bird
Raised Fist0
Djangoframework~20 mins

Custom template filters in Django - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a custom template filter in Django?
easy
A. To modify or format data directly in templates
B. To create new database models
C. To handle user authentication
D. To define URL routing rules

Solution

  1. Step 1: Understand what template filters do

    Template filters change how data appears in templates without changing the data itself.
  2. Step 2: Identify the purpose of custom filters

    Custom filters let you create your own ways to format or modify data in templates.
  3. Final Answer:

    To modify or format data directly in templates -> Option A
  4. Quick Check:

    Custom template filters = modify/format data [OK]
Hint: Filters change data display in templates using pipes [OK]
Common Mistakes:
  • Confusing filters with models or views
  • Thinking filters handle database or routing
  • Assuming filters process user authentication
2. Which of the following is the correct way to register a custom filter in Django?
easy
A. Use @app.route decorator above the filter function
B. Use @register.filter decorator above the filter function
C. Define the filter inside the models.py file
D. Call filter() method on the template object

Solution

  1. Step 1: Recall how to register filters

    Custom filters are registered using the @register.filter decorator from django.template.Library.
  2. Step 2: Eliminate incorrect options

    @app.route is for Flask routing, models.py is for models, and filter() method on template is invalid.
  3. Final Answer:

    Use @register.filter decorator above the filter function -> Option B
  4. Quick Check:

    @register.filter = correct registration [OK]
Hint: Remember: @register.filter decorates filter functions [OK]
Common Mistakes:
  • Using Flask decorators like @app.route
  • Placing filters in models.py instead of templatetags
  • Trying to call filter() on template objects
3. Given this filter code in a file named my_filters.py inside a templatetags folder:
from django import template
register = template.Library()

@register.filter
def double(value):
    return value * 2

What will be the output of {{ 5|double }} in a template?
medium
A. 25
B. Error: filter not found
C. 5double
D. 10

Solution

  1. Step 1: Understand the filter function

    The filter named double multiplies the input value by 2.
  2. Step 2: Apply the filter to the input 5

    5 * 2 equals 10, so the output is 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    5 doubled = 10 [OK]
Hint: Multiply input by 2 for double filter output [OK]
Common Mistakes:
  • Assuming string concatenation instead of multiplication
  • Expecting filter name to appear in output
  • Forgetting to load the filter in template causing error
4. What is wrong with this custom filter code?
from django import template
register = template.Library()

def uppercase(value):
    return value.upper()

register.filter('uppercase', uppercase)
medium
A. The filter must be defined inside models.py
B. The filter function should be named 'upper_case' instead
C. The filter function is not decorated with @register.filter
D. The filter function must return a number

Solution

  1. Step 1: Check filter registration method

    The code uses manual registration with register.filter('uppercase', uppercase). The recommended modern approach is the @register.filter decorator on the function.
  2. Step 2: Eliminate other incorrect options

    Filters are not defined in models.py, the function name is arbitrary, and returning strings is valid.
  3. Final Answer:

    The filter function is not decorated with @register.filter -> Option C
  4. Quick Check:

    Missing @register.filter decorator = non-standard [OK]
Hint: Always use @register.filter decorator for clarity [OK]
Common Mistakes:
  • Thinking function name must be specific
  • Placing filters in models.py instead of templatetags
  • Believing filters must return numbers only
5. You want to create a custom filter that returns the first letter of a string in uppercase, but if the string is empty or null, it should return 'N/A'. Which of these filter implementations is correct?
hard
A. from django import template register = template.Library() @register.filter def first_upper(value): if not value: return 'N/A' return value[0].upper()
B. from django import template register = template.Library() @register.filter def first_upper(value): return value[0].upper()
C. from django import template register = template.Library() @register.filter def first_upper(value): if value == null: return '' return value[0]
D. from django import template register = template.Library() @register.filter def first_upper(value): return value.upper()[0]

Solution

  1. Step 1: Check handling of empty or null values

    The correct one checks if not value (falsy for empty/None) and returns 'N/A'; otherwise value[0].upper().
  2. Step 2: Verify output for valid strings

    For non-empty strings, it returns the first character uppercased.
  3. Step 3: Identify issues in other options

    The second lacks empty check (IndexError on empty). The third uses invalid 'null' (NameError) and no uppercase. The fourth uppercases whole string unnecessarily and errors on empty.
  4. Final Answer:

    The implementation handling empty with 'N/A' and uppercasing first letter -> Option A
  5. Quick Check:

    Check empty first, then uppercase first letter [OK]
Hint: Always check for empty before accessing string index [OK]
Common Mistakes:
  • Not checking for null or empty strings
  • Returning wrong default value
  • Uppercasing entire string unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard