在Django模板中,“load”是什么意思?

26

由于“load”过于通用,因此无法进行搜索:

  1. 在模板文件base_weblog.html中,“load”的目的是什么?它在这种情况下是做什么的?

    {% 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

1
请确保在您的settings.py文件中的INSTALLED_APPS元组中包含了blog.templatetags。这对我来说解决了问题。 - cheenbabes
从 custom_filter.py 文件中加载数据。 - ns15
3个回答

15

6
在模板文件django_website/templates/base_weblog.html中,“load”之后的“weblog”指的是文件夹django_website/apps/blog/templatetags中的weblog.py文件。文件夹templatetags必须以此命名,并且必须包含一个名为__init__.py的文件。(问题2)
“Load”函数可使自定义模板标签(在这种情况下是render_latest_blog_entriesrender_month_links)在模板中使用,例如django_website\templates\base_weblog.html。 “Load”函数具有安全性和性能功能。

0

load tag 可以通过在 load 标签中设置文件来加载 内置自定义 的 Django 模板标签和过滤器。

例如,要创建一个自定义的Django模板标签和过滤器的示例,需要在`core`文件夹中创建一个`templatetags`文件夹,并在其中放置一个名为`__init__.py`(空文件)和一个名为`custom_tags.py`的文件。这个`core`文件夹与`settings.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 还可以使用 python manage.py collectstaticcore 文件夹中的静态文件收集到根 Django 项目文件夹中。我建议在开始构建 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()

然后,您需要通过在加载标签时省略.py来设置custom_tags文件,以便使用如下所示的uppercaselowercase标签:

# "templates/index.html"

{% load custom_tags %}

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

而且,你还可以按照下面的方式创建文件夹,比如app1app2,并在templatetags文件夹中添加__init__.py(空文件)和custom_tags.py。*其他名称也可以用于app1app2文件夹,根据我的实验,如果没有__init__.py(空文件),app1app2文件夹中的custom_tags.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"

然后,使用在load标签中设置的最后一个文件集的uppercase标签。
# "templates/index.html"
                                    #  ↓ ↓ Here ↓ ↓
{% load custom_tags app1.custom_tags app2.custom_tags %}

{% uppercase "Hello World" %} # HELLO WORLD app2

最后,你可以通过多个应用文件夹 app1app2 以及 core 文件夹来创建多个 templatetags 文件夹,如下所示:
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 Admin,没有文件夹可以创建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. 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接