Django模板中的“load”是什么作用?
因为“load”这个词太笼统了,所以在搜索时不太好用:
“load”的目的是什么,它在这个特定的情况下做了什么?- 在一个模板文件,base_weblog.html中,
{% load weblog %}{% render_month_links %}
为了让“load”正常工作,有没有一些命名规则?比如文件夹名、文件名或者类名?
“load”的文档在哪里?能详细解释一下吗?
详细信息:
这个例子来自于http://www.djangoproject.com/ - 直接下载链接是通过http://shrinkster.com/17g8。
部分文件夹结构(没有文件扩展名的项目是文件夹):
django_website
apps
accounts
aggregator
blog
urls.py
models.py
class Entry(models.Model)
templatetags
weblog.py
contact
docs
templates
base_weblog.html
aggregator
blog
entry_archive.html
entry_archive_year.html
month_links_snippet.html
entry_archive_month.html
entry_detail.html
entry_snippet.html
entry_archive_day.html
comments
contact
docs
feeds
flatfiles
flatpages
registration
3 个回答
load 标签可以通过在load
标签中设置文件,来加载Django的内置和自定义模板标签和过滤器。
比如说,创建一个自定义的Django模板标签和过滤器,你需要在core
文件夹(也就是settings.py
所在的地方)里新建一个叫templatetags
的文件夹,并在里面放一个空的__init__.py
文件和一个custom_tags.py
文件,示例如下。然后别忘了重启服务器,这样custom_tags.py
才能应用到Django项目中。*custom_tags.py
可以用其他名字,经过我的实验发现,custom_tags.py
在没有__init__.py
(空文件)的情况下也能工作,但通常来说,最好还是在templatetags
文件夹下创建一个__init__.py
(空文件),这样做是为了遵循文档的建议:
django-project
|-core
| |-settings.py
| └-templatetags # Here
| |-__init__.py
| └-custom_tags.py
|-templates
| └-index.html
|-app1
└-app2
接着,别忘了在settings.py
中把core
添加到INSTALLED_APPS
里,这样custom_tags.py
才能应用到Django项目中,示例如下。*把core
添加到INSTALLED_APPS
也可以将core
文件夹中的静态文件收集到Django项目的根文件夹中,使用命令python manage.py collectstatic
。我建议在开始构建Django项目之前,就把core
添加到INSTALLED_APPS
里:
# "core/settings.py"
INSTALLED_APPS = [
# ...
'core', # Here
'app1',
'app2',
]
然后,在custom_tags.py
中创建uppercase
和lowercase
标签,示例如下。*你可以查看我的回答,里面解释了@register.simple_tag
:
# "core/templatetags/custom_tags.py"
from django.template import Library
register = Library()
@register.simple_tag
def uppercase(arg):
return arg.upper()
@register.filter
def lowercase(arg):
return arg.lower()
接下来,你需要在load标签中设置custom_tags
文件,去掉.py
后缀,这样就可以使用uppercase
和lowercase
标签,示例如下:
# "templates/index.html"
{% load custom_tags %}
{% uppercase "Hello World" %} # HELLO WORLD
{{ "Hello World" | lowercase }} # hello world
此外,你还可以在templatetags
文件夹中创建像app1
和app2
这样的文件夹,并在里面放__init__.py
(空文件)和custom_tags.py
,示例如下。*app1
和app2
文件夹可以用其他名字,经过我的实验发现,app1
和app2
文件夹中的custom_tags.py
在没有__init__.py
(空文件)的情况下是无法工作的,所以必须在app1
和app2
文件夹下创建__init__.py
(空文件):
django-project
|-core
| |-settings.py
| └-templatetags
| |-app1 # Here
| | |-__init__.py
| | └-custom_tags.py
| |-app2 # Here
| | |-__init__.py
| | └-custom_tags.py
| |-__init__.py
| └-custom_tags.py
|-templates
| └-index.html
|-app1
└-app2
然后,你需要在load标签中设置custom_tags
文件,按照app1
和app2
文件夹的结构,这样就可以使用在app1/custom_tags.py
和app2/custom_tags.py
中定义的标签,示例如下:
# "templates/index.html"
# ↓ ↓ Here ↓ ↓ ↓ ↓ Here ↓ ↓
{% load custom_tags app1.custom_tags app2.custom_tags %}
# ...
如果在多个文件中有相同的标签,比如uppercase
,示例如下:
# "core/templatetags/custom_tags.py"
@register.simple_tag
def uppercase(arg):
return arg.upper()
# "core/templatetags/app1/custom_tags.py"
@register.simple_tag
def uppercase(arg):
return arg.upper() + " app1"
# "core/templatetags/app2/custom_tags.py"
@register.simple_tag
def uppercase(arg):
return arg.upper() + " app2"
那么,最后一个文件中设置的uppercase
标签会被使用。
# "templates/index.html"
# ↓ ↓ Here ↓ ↓
{% load custom_tags app1.custom_tags app2.custom_tags %}
{% uppercase "Hello World" %} # HELLO WORLD app2
最后,你可以在多个应用文件夹app1
和app2
中创建多个templatetags
文件夹,除了core
文件夹,示例如下:
django-project
|-core
| |-settings.py
| └-templatetags # Here
| |-__init__.py
| └-custom_tags.py
|-templates
| └-index.html
|-app1
| └-templatetags # Here
| |-__init__.py
| └-custom_tags.py
└-app2
└-templatetags # Here
|-__init__.py
└-custom_tags.py
不过,我建议只在core
文件夹中创建一个templatetags
文件夹,因为有以下三个原因:
管理一个
core
文件夹中的templatetags
文件夹比管理多个应用文件夹中的templatetags
文件夹要简单。对于Django管理后台,没有地方可以创建
templatetags
文件夹,所以core
文件夹是最合理的选择。
第三个原因是,如果在多个templatetags
文件夹中有多个同名文件,比如custom_tags.py
,示例如下:
django-project
|-core
| |-settings.py
| └-templatetags
| |-__init__.py
| └-custom_tags.py # Here
|-templates
| └-index.html
|-app1
| └-templatetags
| |-__init__.py
| └-custom_tags.py # Here
└-app2
└-templatetags # Here
|-__init__.py
└-custom_tags.py # Here
那么,Django模板中只会加载一个文件,但我不知道到底会加载哪个文件,经过我的实验如下:
# "templates/index.html"
{% load custom_tags %} # I don't know which `custom_tags.py` is loaded.
在模板文件 django_website/templates/base_weblog.html
中的 "load" 后面提到的 "weblog",是指在 django_website/apps/blog/templatetags
文件夹里的 weblog.py
文件。这个 templatetags
文件夹必须准确地命名为这个名字,并且里面必须有一个叫 __init__.py
的文件(这是第二个问题)。
"load" 的作用是让自定义的模板标签(在这个例子中是 render_latest_blog_entries
和 render_month_links
)可以在模板中使用,也就是在 django_website/templates/base_weblog.html
中使用。"load" 还起到安全和性能的作用。