Templates settings in Python's Django framework

Source: Internet
Author: User
Tags auth empty

This article mainly introduces the templates settings in Python's Django framework, mainly describes some of the new features after the Django1.8 version, the need for friends can refer to the


New features of Django 1.8

A list that contains all the settings for the template engine used in Django. Each item in the list is a dictionary that contains an option for an engine.

Here's a simple setup that tells the Django template engine to read the template from the Templates subdirectory of the installed application (installed applications):


1 2 3 4 5 6 TEMPLATES = [{' Backend ': ' django.template.backends.django.DjangoTemplates ', ' app_dirs ': True,},]

The following options are available for all engines (backends).


Default: No definition

The template engine used. The built-in template engines are:


1 2 ' Django.template.backends.django.DjangoTemplates ' django.template.backends.jinja2.Jinja2 '

By setting backend as a complete (fully-qualified) path (for example, ' Mypackage.whatever. Backend '), you can use a non-Django self-brought engine.


Default: Look below

The alias of the template engine. It is an identifier that allows you to select an engine when rendering. Aliases must be unique in all configured template engines.

When a value is not supplied, the default is to define the template name of the engine class, which is the last part adjacent to the backend.

For example if the engine is ' mypackage.whatever. Backend ', then its default name is ' whatever '.


Default: [] (Empty list)

The directories that the engine uses to find template source files, sorted by search order.


Default: False

Whether the engine locates the template source file within the installed application (directory).


Default: {} (Empty dictionary)

Other parameters passed to the template engine (backend). Different engines, the available parameters are not the same.




1 2 3 4 5 6 7 ("Django.contrib.auth.context_processors.auth", "Django.template.context_processors.debug", " django.template.context_processors.i18n "," Django.template.context_processors.media "," Django.template.context_ Processors.static "," Django.template.context_processors.tz "," Django.contrib.messages.context_ Processors.messages ")

Since version 1.8, it is not in favor of using:

Options in a djangotemplates engine are set to replace the ' context_processors ' option.

The tuple of the invocation function (Callables) that is used to populate the context in RequestContext. These functions take a request object as its parameter and return a dictionary that will be populated to the context item.

The changes to Django 1.8:

In Django 1.8, the context processor for the built-in template is moved from django.core.context_processors to Django.template.context_processors.


Default: False

Since version 1.8, it is not in favor of using:

Options in a djangotemplates engine are set to replace the ' debug ' option.

A Boolean value that turns on/off the template debug mode. If the value is true, throwing any exceptions during template rendering will display a lovely, detailed report error page. The page contains the code snippets associated with the template, and the appropriate line highlighting is used.

Note that if debug is True,django only the cute error page will be displayed.

See also DEBUG.


Default: () (Empty list)

Since version 1.8, it is not in favor of using:

Set the ' dirs ' option in a djangotemplates engine instead.

Django.template.loaders.filesystem.Loader Search the path list for the source code of the template, sorted by search order.

Note These paths are also used in Unix-style forward slashes even in Windows.

See the Django template language.




1 2 (' Django.template.loaders.filesystem.Loader ', ' django.template.loaders.app_directories. Loader ')

Since version 1.8, it is not in favor of using:

Options in a djangotemplates engine are set to replace the ' loader ' option.

The tuple of the template reader class, specified with a string. Each reader class knows how to import a template from a specific source (particular source). Optionally, you can use a tuple instead of a string. The first item in the tuple should be the reader's module, and subsequent items are passed to the reader at initialization time. See the Django template language:for Python programmers.


Default: ' (empty string)

Since version 1.8, it is not in favor of using:

Options in a djangotemplates engine are set to replace the ' string_if_invalid ' option.

A string that is output by the template system when an unavailable (for example, spelling error) variable is used. See how invalid variables are handled.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.