Django模板中的“load”是什么作用?

26 投票
3 回答
21281 浏览
提问于 2025-04-15 12:30

因为“load”这个词太笼统了,所以在搜索时不太好用:

  1. “load”的目的是什么,它在这个特定的情况下做了什么?- 在一个模板文件,base_weblog.html中,

    {% load weblog %}{% render_month_links %}

  2. 为了让“load”正常工作,有没有一些命名规则?比如文件夹名、文件名或者类名?

  3. “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 个回答

0

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中创建uppercaselowercase标签,示例如下。*你可以查看我的回答,里面解释了@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后缀,这样就可以使用uppercaselowercase标签,示例如下:

# "templates/index.html"

{% load custom_tags %}

{% uppercase "Hello World" %} # HELLO WORLD
{{ "Hello World" | lowercase }} # hello world

此外,你还可以在templatetags文件夹中创建像app1app2这样的文件夹,并在里面放__init__.py(空文件)和custom_tags.py,示例如下。*app1app2文件夹可以用其他名字,经过我的实验发现,app1app2文件夹中的custom_tags.py在没有__init__.py(空文件)的情况下是无法工作的,所以必须在app1app2文件夹下创建__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文件,按照app1app2文件夹的结构,这样就可以使用在app1/custom_tags.pyapp2/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

最后,你可以在多个应用文件夹app1app2中创建多个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文件夹,因为有以下三个原因:

  1. 管理一个core文件夹中的templatetags文件夹比管理多个应用文件夹中的templatetags文件夹要简单。

  2. 对于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. 
15

load:

加载一个自定义的模板标签集合。

想了解更多信息,可以查看自定义标签和过滤器库

6

在模板文件 django_website/templates/base_weblog.html 中的 "load" 后面提到的 "weblog",是指在 django_website/apps/blog/templatetags 文件夹里的 weblog.py 文件。这个 templatetags 文件夹必须准确地命名为这个名字,并且里面必须有一个叫 __init__.py 的文件(这是第二个问题)。

"load" 的作用是让自定义的模板标签(在这个例子中是 render_latest_blog_entriesrender_month_links)可以在模板中使用,也就是在 django_website/templates/base_weblog.html 中使用。"load" 还起到安全和性能的作用。

撰写回答