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 Filter

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.

  • Using built-in filters: Django includes many built-in filters, such as 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.

  • Chaining filters: You can chain multiple filters together by using the | (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.

  • Creating a custom filter: If you need a filter that isn't available by default, you can create a custom filter. First, create a new folder named 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.

  • Using a custom filter in a template: To use a custom filter in a template, you first need to load the filter using the {% 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.

  1. List of Django Built-In Template Filters:

    • Description: Django provides a variety of built-in template filters for formatting and manipulating data in templates.
    • Code: Examples of some built-in filters:
      {{ variable|default:"No value" }}
      {{ value|date:"F j, Y" }}
      {{ string|lower }}
      
  2. 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>
      
  3. Using Filters in Django Templates:

    • Description: Apply filters to variables in Django templates to modify their presentation or content.
    • Code: Example of using filters in a Django template:
      <p>{{ variable|filter_name }}</p>
      
  4. Filtering Data in Django Templates:

    • Description: Filter and display specific data in Django templates using built-in or custom filters.
    • Code: Example of filtering data in a Django template:
      {% for item in my_list|filter_condition %}
          <p>{{ item.name }}</p>
      {% endfor %}
      
  5. Common Django Template Filters:

    • Description: Explore commonly used Django template filters, such as date, default, lower, length, and more.
    • Code: Examples of using common filters in Django templates:
      {{ value|date:"F j, Y" }}
      {{ text|lower }}
      {{ items|length }}
      
  6. Chaining Filters in Django Templates:

    • Description: Chain multiple filters to apply a series of transformations to a variable in Django templates.
    • Code: Example of chaining filters in a Django template:
      <p>{{ variable|filter1|filter2 }}</p>
      
  7. Django Template Date Filters:

    • Description: Use date filters in Django templates to format and display date and time values.
    • Code: Examples of date filters in Django templates:
      {{ my_date|date:"F j, Y" }}
      {{ my_datetime|time:"h:i A" }}
      
  8. Numeric Filters in Django Templates:

    • Description: Numeric filters in Django templates allow formatting and manipulating numeric values.
    • Code: Example of numeric filters in a Django template:
      <p>{{ price|currency }}</p>
      <p>{{ quantity|floatformat:2 }}</p>
      
  9. String Manipulation Filters in Django Templates:

    • Description: String manipulation filters in Django templates enable modifying and formatting string values.
    • Code: Examples of string manipulation filters:
      {{ text|lower }}
      {{ sentence|title }}
      
  10. Applying Filters to Variables in Django Templates:

    • Description: Apply filters directly to variables in Django templates to modify their presentation or content.
    • Code: Example of applying filters to variables:
      <p>{{ variable|filter1|filter2 }}</p>
      
  11. Filtering Queryset Data in Django Templates:

    • Description: Filter queryset data directly in Django templates using built-in or custom filters.
    • Code: Example of filtering queryset data in a Django template:
      {% for item in my_queryset|filter_condition %}
          <p>{{ item.name }}</p>
      {% endfor %}