模板
Django项目可以配置一个或多个模板引擎(或者如果不使用模板,甚至为零)。Django后端内置一个自己的模板系统,创造性地称为Django template language(DTL),和一个流行的替代品JICAN2*。后端也可以使用第三方提供其他可用的模板语言。
由于历史原因,模板引擎的通用支持和Django模板语言的实现都存在于django.template
模块的命名空间中。
模板引擎的支持
配置
Templates engines are configured with the TEMPLATES
setting. It's a list of configurations, one for each engine. The default value is empty. The settings.py
generated by the startproject
command defines a more useful value:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
},
},
]
BACKEND
is a dotted Python path to a template engine class implementing Django's template backend API. The built-in backends are django.template.backends.django.DjangoTemplates
and django.template.backends.jinja2.Jinja2
.
Since most engines load templates from files, the top-level configuration for each engine contains two common settings:
-
DIRS
defines a list of directories where the engine should look for template source files, in search order. -
APP_DIRS
tells whether the engine should look for templates inside installed applications. Each backend defines a conventional name for the subdirectory inside applications where its templates should be stored.
While uncommon, it's possible to configure several instances of the same backend with different options. In that case you should define a unique NAME
for each engine.
OPTIONS
contains backend-specific settings.
The Django template language
Syntax
A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags.
A template is rendered with a context. Rendering replaces variables with their values, which are looked up in the context, and executes tags. Everything else is output as is.
The syntax of the Django template language involves four constructs.
变量
A variable outputs a value from the context, which is a dict-like object mapping keys to values.
Variables are surrounded by {{ and }} like this:
My first name is {{ first_name }}. My last name is {{ last_name }}.
With a context of {'first_name': 'John', 'last_name': 'Doe'}, this template renders to:
My first name is John. My last name is Doe.
Dictionary lookup, attribute lookup and list-index lookups are implemented with a dot notation:
{{ my_dict.key }}
{{ my_object.attribute }}
{{ my_list.0 }}
If a variable resolves to a callable, the template system will call it with no arguments and use its result instead of the callable.
标签(Tags)
Tags provide arbitrary logic in the rendering process.
This definition is deliberately vague. For example, a tag can output content, serve as a control structure e.g. an "if" statement or a "for" loop, grab content from a database, or even enable access to other template tags.
Tags are surrounded by {% and %} like this:
{% csrf_token %}
Most tags accept arguments:
{% cycle 'odd' 'even' %}
Some tags require beginning and ending tags:
{% if user.is_authenticated %}Hello, {{ user.username }}.{% endif %}
A reference of built-in tags is available as well as instructions for writing custom tags.
过滤器
Filters transform the values of variables and tag arguments.
They look like this:
{{ django|title }}
With a context of {'django': 'the web framework for perfectionists with deadlines'}, this template renders to:
The Web Framework For Perfectionists With Deadlines
Some filters take an argument:
{{ my_date|date:"Y-m-d" }}
A reference of built-in filters is available as well as instructions for writing custom filters.
注释(Comments)
Comments look like this:
{# this won't be rendered #}
A {% comment %}
tag provides multi-line comments.
网友评论