A custom template filter is a user-defined function that processes data in Django templates to change how values are displayed. It lets you create your own way to modify or format data inside templates.
Click to reveal answer
beginner
How do you register a custom template filter in Django?
You create a Python function and register it with Django's template library using the @register.filter decorator inside a file in the 'templatetags' folder. This makes the filter available in templates.
Click to reveal answer
intermediate
Where should you place your custom template filter code in a Django project?
Inside a 'templatetags' folder within a Django app. This folder must contain an __init__.py file to be recognized as a module.
Click to reveal answer
beginner
What is the purpose of the @register.filter decorator in Django custom filters?
It tells Django that the function below it is a template filter and makes it available to use inside templates.
Click to reveal answer
intermediate
Can custom template filters accept arguments? How?
Yes, custom filters can accept extra arguments by defining them in the function parameters after the value. You can pass these arguments in the template using a colon, like {{ value|filtername:arg }}.
Click to reveal answer
Where do you put custom template filters in a Django app?
AIn the static files folder
BInside a 'templatetags' folder with __init__.py
CDirectly inside views.py
DIn the main project folder
✗ Incorrect
Custom template filters must be placed inside a 'templatetags' folder within a Django app, which must have an __init__.py file.
How do you make Django recognize a function as a template filter?
AUse @register.filter decorator
BName the function 'filter'
CImport it in settings.py
DAdd it to urls.py
✗ Incorrect
The @register.filter decorator registers the function as a template filter in Django.
What does a custom template filter do?
AModifies or formats data in templates
BHandles HTTP requests
CDefines database models
DManages user authentication
✗ Incorrect
Custom template filters change how data is displayed in templates by modifying or formatting it.
How do you pass an argument to a custom template filter in a template?
A{{ filtername(value, argument) }}
B{{ value|filtername(argument) }}
C{% filter filtername argument %}
D{{ value|filtername:argument }}
✗ Incorrect
Arguments are passed to filters in templates using a colon, like {{ value|filtername:argument }}.
What file must exist inside the 'templatetags' folder for Django to recognize it?
Aviews.py
Bfilters.py
C__init__.py
Dmodels.py
✗ Incorrect
The __init__.py file makes the 'templatetags' folder a Python module so Django can load filters from it.
Explain how to create and use a custom template filter in Django from start to finish.
Think about folder structure, function, registration, loading, and usage.
You got /5 concepts.
Describe how to pass an argument to a custom template filter and how to handle it in the filter function.
Focus on function parameters and template syntax.
You got /3 concepts.
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
Step 1: Understand what template filters do
Template filters change how data appears in templates without changing the data itself.
Step 2: Identify the purpose of custom filters
Custom filters let you create your own ways to format or modify data in templates.
Final Answer:
To modify or format data directly in templates -> Option A
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
Step 1: Recall how to register filters
Custom filters are registered using the @register.filter decorator from django.template.Library.
Step 2: Eliminate incorrect options
@app.route is for Flask routing, models.py is for models, and filter() method on template is invalid.
Final Answer:
Use @register.filter decorator above the filter function -> Option B
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
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.
Step 2: Eliminate other incorrect options
Filters are not defined in models.py, the function name is arbitrary, and returning strings is valid.
Final Answer:
The filter function is not decorated with @register.filter -> Option C
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
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().
Step 2: Verify output for valid strings
For non-empty strings, it returns the first character uppercased.
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.
Final Answer:
The implementation handling empty with 'N/A' and uppercasing first letter -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.