What is the Jinja reject Filter?
nThe Jinja reject filter is a powerful tool used to filter a sequence of objects. It’s the opposite of the select filter: instead of keeping items that pass a test, reject keeps only the items that fail the test. This filter is great for removing unwanted items from a list, such as zero values, or any other data that doesn’t meet a specific criterion. By using reject, you can keep your templates clean and focused on displaying the data you want, without needing to clutter your code with complex conditional logic.nn
nn
How the reject Filter Works
nThe reject filter takes an iterable (like a list) and a test as arguments. It then applies the test to each item in the list. Any item for which the test returns True is rejected, and any item for which the test returns False is kept.nnThe basic syntax is as follows:n
{{ my_list | reject('test_name', test_argument) }}
n
- n
my_list: The iterable you want to filter.reject: The filter itself.'test_name': The name of the built-in or custom test to apply to each item.test_argument: An optional argument for the test.
n
n
n
n
nIf you don’t provide a test, the filter defaults to a boolean evaluation. This means it rejects any item that is “truthy” in Python (e.g., non-zero numbers, non-empty strings, True) and keeps items that are “falsy” (0, '', None, False). This default behavior is particularly useful for cleaning up lists of data that might contain empty or null values.nnThe reject filter returns an iterator, which is a memory-efficient way of handling the filtered items. This means it processes items one by one as they are requested, rather than building a new list in memory. For this reason, you’ll typically use this filter within a for loop.nn
nn
Practical Examples
nLet’s look at some practical examples to see the reject filter in action.n
Example 1: Removing Odd Numbers
nThis example demonstrates how to remove items that pass the 'odd' test.nnPython Code:n
# Assuming 'numbers' is passed to the templatennumbers = [1, 2, 3, 4, 5, 6]
nJinja Template:n
<h2>Even Numbers</h2>n<ul>n{% for number in numbers | reject('odd') %}n <li>{{ number }}</li>n{% endfor %}n</ul>
nRendered HTML:n
<h2>Even Numbers</h2>n<ul>n <li>2</li>n <li>4</li>n <li>6</li>n</ul>
nThe reject('odd') filter successfully removes the numbers 1, 3, and 5 because they pass the odd test.n
Example 2: Using the Default Boolean Evaluation
nThis example demonstrates how to use the default Boolean evaluation to filter a list.nnPython Code:n
# Assuming 'values' is passed to the templatenvalues = [10, 0, 20, 0, 30]
nJinja Template:n
<h2>Zero Values</h2>n<ul>n{% for value in values | reject %}n <li>{{ value }}</li>n{% endfor %}n</ul>
nRendered HTML:n
<h2>Zero Values</h2>n<ul>n <li>0</li>n <li>0</li>n</ul>
nIn this case, the reject filter rejects the truthy values 10, 20, and 30, keeping only the “falsy” zeros.nn
nn
Common Mistakes
nA common mistake is confusing reject with select. Remember:n
- n
selectkeeps items that pass the test.rejectkeeps items that fail the test.
n
n
nAnother common mistake is to try and use the filter to modify the original list. Jinja filters are non-destructive and always return a new sequence.nn
nn
Conclusion
nThe Jinja reject filter is an excellent tool for template developers who need to quickly and efficiently remove unwanted items from an iterable. By providing a declarative way to exclude items based on a simple test, it helps you write cleaner, more readable, and more maintainable templates. By understanding its inverse relationship with the select filter and its default boolean behavior, you can leverage the power of reject to present your data exactly how you want it.nn
