A tutorial on the setup of templates in Python's Django framework

Source: Internet
Author: User
Tags auth in python

This article mainly introduces the templates in Python's Django Framework tutorial, mainly for the new features after Django1.8, need friends can refer to the following

TEMPLATES

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).

Backend

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.

NAME

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 '.

Dirs

Default: [] (Empty list)

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

App_dirs

Default: False

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

OPTIONS

Default: {} (Empty dictionary)

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

Template_context_processors

Default:

?

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.

Template_debug

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.

Template_dirs

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.

Template_loaders

Default:

?

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.

Template_string_if_invalid

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.

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.