Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze how to write custom template tags tags and filters in Django

Shulou Source: shulou.com Published: 2022-06-01 16:14:57 09月28日 Update

This article introduces how to analyze how to write custom template tags tags and filters in Django, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Django template system comes with a series of built-in tags and filters, generally can meet your requirements, if you feel the need for more accurate template tags or filters, you can write your own template tags and filters, and then use the {% load%} tag to use them.

Code layout

Custom tags and filters must depend on a django app, that is, custom tags and filters are bound to app. The app should contain a templatetags directory, which is at the same level as model.py,views.py. Remember to create a _ _ init__.py file in this directory and django knows that this is a python package. In this directory, you can create a new python module file with a file name that does not conflict with other app. For example:

Polls/ models.py templatetags/ _ _ init__.py poll_extras.py views.py

Then you can use your custom tags and filters in your template file like this:

{% load poll_extras%}

Note:

The app containing the templatetags directory must be in the INSTALLED_APPS list

{% load%} load is the module name, not the app name

Remember to register with from django import template, register=template.Library ()

On how to analyze Django in the preparation of custom template tags tags and filter to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Tags filters templates directories files analysis content more modules help yes precision that is that is matters code interests guys partners levels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Linux Docker MySQL