Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
Django template filters are used to modify variables within templates. They can be used for formatting, transforming data, and applying various operations on the variables. In this tutorial, we'll cover the basics of using built-in Django template filters and creating custom filters.
lower
, upper
, truncatechars
, date
, and more. To use a filter, insert the |
(pipe) character followed by the filter name within the double curly braces {{ }}
. Here's an example:<!DOCTYPE html> <html> <head> <title>My Django App</title> </head> <body> <h1>{{ title|upper }}</h1> <p>{{ description|truncatechars:30 }}</p> <p>Posted on {{ post_date|date:"F j, Y" }}</p> </body> </html>
In this example, the upper
filter converts the title
variable to uppercase, the truncatechars
filter shortens the description
variable to 30 characters, and the date
filter formats the post_date
variable.
|
(pipe) character. Filters are applied from left to right. Here's an example:<p>{{ description|truncatewords:10|lower }}</p>
In this example, the truncatewords
filter shortens the description
variable to 10 words, and then the lower
filter converts the resulting string to lowercase.
templatetags
within your Django app. Inside this folder, create a new file named my_filters.py
. To define a custom filter, create a function that takes one or two arguments (the variable value and an optional argument) and returns the modified value. Use the @register.filter
decorator to register your custom filter. Here's an example:from django import template register = template.Library() @register.filter def multiply(value, arg): return value * arg
This custom filter, multiply
, takes a value and an argument and returns the product of the two.
{% load %}
tag. Then, use the filter as you would with built-in filters:{% load my_filters %} <!DOCTYPE html> <html> <head> <title>My Django App</title> </head> <body> <p>Price: ${{ price|multiply:1.1|floatformat:2 }}</p> </body> </html>
In this example, the multiply
filter increases the price
variable by 10% (multiplies by 1.1), and the floatformat
filter formats the result as a floating-point number with two decimal places.
By using built-in and custom filters in Django templates, you can easily modify and transform variable values, enhancing the flexibility and functionality of your application's presentation logic.
List of Django Built-In Template Filters:
{{ variable|default:"No value" }} {{ value|date:"F j, Y" }} {{ string|lower }}
How to Create Custom Django Template Filters:
Description: Create custom template filters in Django by defining a Python function and registering it with the template system.
Code: Example of creating a custom filter:
# custom_filters.py from django import template register = template.Library() @register.filter(name='double') def double(value): return value * 2
<!-- template.html --> <p>{{ some_variable|double }}</p>
Using Filters in Django Templates:
<p>{{ variable|filter_name }}</p>
Filtering Data in Django Templates:
{% for item in my_list|filter_condition %} <p>{{ item.name }}</p> {% endfor %}
Common Django Template Filters:
date
, default
, lower
, length
, and more.{{ value|date:"F j, Y" }} {{ text|lower }} {{ items|length }}
Chaining Filters in Django Templates:
<p>{{ variable|filter1|filter2 }}</p>
Django Template Date Filters:
{{ my_date|date:"F j, Y" }} {{ my_datetime|time:"h:i A" }}
Numeric Filters in Django Templates:
<p>{{ price|currency }}</p> <p>{{ quantity|floatformat:2 }}</p>
String Manipulation Filters in Django Templates:
{{ text|lower }} {{ sentence|title }}
Applying Filters to Variables in Django Templates:
<p>{{ variable|filter1|filter2 }}</p>
Filtering Queryset Data in Django Templates:
{% for item in my_queryset|filter_condition %} <p>{{ item.name }}</p> {% endfor %}