Earlier, I explained how to use Django’s built-in filters, which come in handy when you’re working on some projects. However, the built-in filters are limited (what happens when the built-in filters don’t meet our needs?). But rules are dead and people are alive. This brings us to the next section: custom filters and labels.

First, understand two questions:

  • What is the custom filter and label – according to certain rules, their own definition to meet the requirements of the function;
  • Customize where filters and labels are used – wherever you need them;

In fact, you can think about why you use custom functions, custom modules! It’s all the same.

1. Configure file paths for user-defined filters and labels:

We all know that Django dictates that template files must be placed in the Templates directory; Django also specifies custom filters and tags that must be placed in the templateTags directory!

2. Two points to note about customizing filters and labels:

The first point:

Custom filters and tags must be in the TemplateTags directory!

The second point:

The templateTags directory must be included in the registered app! (Register your app in the settings.py file in the project directory.)

3. There are two templateTags directories for custom filters and tags:

The first type: special use of an app (exclusive)

  1. In the app folder, in the templateTags folder (the __init__.py file is required)

  2. Public – Create a new app and create a templateTags folder in the new app (note: this app must be registered!).