Django自动的扩展jinja2模板的功能并不好用,还是使用django-jinja插件比较靠谱。
1、安装Jinja2模块:
1
|
pip
install
django-jinja
|
2、配置settings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
INSTALLED_APPS += (
'django_jinja'
,)
TEMPLATES = [
{
"BACKEND"
:
"django_jinja.backend.Jinja2"
,
"APP_DIRS"
: True,
"OPTIONS"
: {
# Match the template names ending in .html but not the ones in the admin folder.
"match_extension"
:
".html"
,
"match_regex"
: r
"^(?!admin/).*"
,
"app_dirname"
:
"jinja2"
,
# Can be set to "jinja2.Undefined" or any other subclass.
"undefined"
: None,
"context_processors"
: [
"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"
,
],
"extensions"
: [
"jinja2.ext.do"
,
"jinja2.ext.loopcontrols"
,
"jinja2.ext.with_"
,
"jinja2.ext.i18n"
,
"jinja2.ext.autoescape"
,
"django_jinja.builtins.extensions.CsrfExtension"
,
"django_jinja.builtins.extensions.CacheExtension"
,
"django_jinja.builtins.extensions.TimezoneExtension"
,
"django_jinja.builtins.extensions.UrlsExtension"
,
"django_jinja.builtins.extensions.StaticFilesExtension"
,
"django_jinja.builtins.extensions.DjangoFiltersExtension"
,
],
"bytecode_cache"
: {
"name"
:
"default"
,
"backend"
:
"django_jinja.cache.BytecodeCache"
,
"enabled"
: False,
},
"autoescape"
: True,
"auto_reload"
: DEBUG,
"translation_engine"
:
"django.utils.translation"
,
}
},
{
'BACKEND'
:
'django.template.backends.django.DjangoTemplates'
,
'DIRS'
: [],
'APP_DIRS'
: True,
'OPTIONS'
: {
'context_processors'
: [
'django.template.context_processors.debug'
,
'django.template.context_processors.request'
,
'django.contrib.auth.context_processors.auth'
,
'django.contrib.messages.context_processors.messages'
,
],
},
},
]
|
模板目录用jinja2
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1836608如需转载请自行联系原作者
lihuipeng