Describes how to create a template library in the Python Django framework.

Source: Internet
Author: User

Describes how to create a template library in the Python Django framework.

Whether writing custom tags or filters, the first thing to do is to create a template library (the basic structure that Django can import ).

Create a template library in two steps:

First, decide the Django application under which the template library should be placed. If you use manage. py startapp to create an application, you can put it there, or you can create an application for the template library separately. We recommend the latter because your filter may be useful in later projects.

Regardless of the method you use, make sure to add your application to INSTALLED_APPS. We will explain this later.

Second, create a templatetags directory in the appropriate Django application package. This directory should be at the same level as models. py and views. py. For example:

books/  __init__.py  models.py  templatetags/  views.py

Create two empty files in templatetags: One _ init __. py (tells Python that this is a package containing Python code) and a file used to store your custom tags/filter definitions. The name of the Second file will be used to load labels later. For example, if your custom tag/filter is in a file named poll_extras.py, you need to write the following content in the template:

{% load poll_extras %}

The {% load %} label checks the settings in INSTALLED_APPS. Only the template library in the installed Django application can be loaded. This is a security feature. It allows you to deploy a lot of template library code on a computer without exposing them to every Django installation.

If you write a template library that is not associated with any specific model/view, it is completely normal to get a Django application package containing only the templatetags package. No restrictions are imposed on how many modules are placed in the templatetags package. Note that the {% load %} statement loads tags/filters by the specified Python Module name rather than the application name.

Once the Python module is created, you only need to write some Python code based on the filter or tag.

As a legal tag library, a module must contain a module-level variable named register. This variable is an instance of template. Library and is the data structure of all registration tags and filters. Therefore, insert the following statement at the top of your module:

from django import templateregister = template.Library()

Note:

Read Django's default filter and tag source code. There are a lot of examples. They are django/template/defaultfilters. py and django/template/defaulttags. py. Some applications in django. contrib also contain the template library.

After creating the register variable, you can use it to create the filter and tag of the template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.