Django Template Filters

Django template filters are simple Python functions that modify the values of variables for display in templates. They are used within Django’s template language to transform data before rendering it to the user. Filters can be applied using the pipe (|) symbol and can take optional arguments.

Here’s a basic example of a Django template filter:

Suppose you have a variable called name in your template, and you want to display it in all uppercase letters. Django has a built-in filter called upper that can do this. Here’s how you might use it in a template:

<!-- Assuming 'name' has the value 'Mahmood' -->
<p>Your name in uppercase is: {{ name|upper }}</p>

This would render as:

<p>Your name in uppercase is: MAHMOOD</p>

This example demonstrates how to use the upper filter to convert the text of a variable to uppercase before rendering it on a webpage. Django provides a variety of built-in filters for common tasks like formatting dates, handling strings, and manipulating arrays. You can also create your own custom filters if you need functionality that isn’t provided by the built-in options.