“加载”是什么意思? 在 Django 模板中做什么?

发布于 2024-07-25 17:18:22 字数 1123 浏览 10 评论 0 原文

由于“load”对于搜索来说太通用了:

  1. “load”的目的是什么?在这种特殊情况下它有什么作用? - 在模板文件中,base_weblog.html,

    {% 加载博客 %}{% render_month_links %}

  2. 是否使用了一些命名约定,以便“加载” 做好它的工作吗? 例如文件夹和/或文件的名称和/或 类名?

  3. “加载”的文档在哪里?您能详细说明吗?


详细信息:

该示例来自以下来源 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

As "load" is far too generic for searching:

  1. What is the purpose of "load" and what does it do in this particular case? - in a template file, base_weblog.html,

    {% load weblog %}{% render_month_links %}

  2. Are some naming conventions used in order for "load" to
    do its job? E.g. names of folders and/or files and/or
    class names?

  3. Where is the documentation for "load" and can you elaborate?


Details:

The example is from the source for
http://www.djangoproject.com/ - direct download URL is
through http://shrinkster.com/17g8.

Partial folder structure (items with no file extension are folders):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼睛会笑 2024-08-01 17:18:22

load

加载自定义模板标签集。

请参阅自定义标签和过滤器库 了解更多信息。

load:

Load a custom template tag set.

See Custom tag and filter libraries for more information.

浮云落日 2024-08-01 17:18:22

“load”之后的“weblog”(在模板文件 django_website/templates/base_weblog.html 中)引用文件夹 django_website/apps/blog/ 中的文件 weblog.py模板标签。 文件夹 templatetags 必须准确命名,并且必须包含名为 __init__.py 的文件(问题 2)。

“load”使自定义模板标签(在本例中为 render_latest_blog_entriesrender_month_links)可在模板 django_website\templates\base_weblog.html 中使用在这种情况下。 “负载”是一个安全和性能功能。

"weblog" after "load" (in template file django_website/templates/base_weblog.html) refers to file weblog.py in folder django_website/apps/blog/templatetags. Folder templatetags must be named exactly that and must contain a file named __init__.py (question 2).

"load" makes the custom template tags (render_latest_blog_entries and render_month_links in this case) available for use in templates, django_website\templates\base_weblog.html in this case. "Load" is a security and performance function.

倾其所爱 2024-08-01 17:18:22

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

例如自定义 Django 模板标签和过滤器,在 core 文件夹中创建 templatetags 文件夹,其中包含 __init__.py(空文件)和 custom_tags.py,其中 settings.py如下所示,然后不要忘记重新启动服务器以将custom_tags.py应用到Django项目。 *其他名称对于 custom_tags.py 来说也可以,根据我的实验,custom_tags.py 可以在没有 __init__.py(空文件)的情况下工作,但基本上,最好按照 文档 说:

django-project
 |-core
 |  |-settings.py
 |  └-templatetags # Here
 |     |-__init__.py
 |     └-custom_tags.py
 |-templates
 |  └-index.html
 |-app1
 └-app2

并且,不要忘记将 core 设置为 settings.py 中的 >INSTALLED_APPS 将 custom_tags.py 应用到 Django 项目,如下所示。 *将core设置为INSTALLED_APPS也可以使用python manage.pycollectstatic将core文件夹中的静态文件收集到Django项目根文件夹中。 我建议在开始构建 Django 项目之前将 core 设置为 INSTALLED_APPS

# "core/settings.py"

INSTALLED_APPS = [
    # ...
    'core', # Here
    'app1',
    'app2',
]

然后,创建 uppercaselowercase 标签在 custom_tags.py 中,如下所示。 *您可以看到我的答案解释@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 文件夹,根据我的实验,custom_tags.py 位于 app1 和如果没有 __init__.py(空文件),app2 文件夹将无法工作,因此必须在 __init__.py(空文件)下创建>app1 和 app2 文件夹:

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

然后,您需要设置文件夹 app1app2< 后面的文件 custom_tags /code> 在 load 标签中使用 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

最后,除了 core 文件夹之外,您还可以通过多个应用程序文件夹 app1app2 创建多个 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 文件夹,因为有以下 3 个原因:

  1. 只管理一个 templatetags 文件夹更容易管理core 文件夹中的 code>templatetags 文件夹比通过多个应用文件夹管理多个 templatetags 文件夹要好。

  2. 对于 Django Admin,没有用于创建 templatetags 文件夹的文件夹,因此 core 文件夹是 Django Admin 执行此操作的最合理位置。

第三个原因是因为通过多个 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. 

load tag can load built-in and custom Django template tags and filters by setting the files in load tag.

For example of a custom Django template tag and filter, create templatetags folder with __init__.py(Empty file) and custom_tags.py in core folder where settings.py is as shown below, then don't forget to restart server to apply custom_tags.py to Django project. *Other names are fine for custom_tags.py and according to my experiments, custom_tags.py works without __init__.py(Empty file) but basically, it is better to create __init__.py(Empty file) just under templatetags folder by following what the doc says:

django-project
 |-core
 |  |-settings.py
 |  └-templatetags # Here
 |     |-__init__.py
 |     └-custom_tags.py
 |-templates
 |  └-index.html
 |-app1
 └-app2

And, don't forget to set core to INSTALLED_APPS in settings.py to apply custom_tags.py to Django project as shown below. *Setting core to INSTALLED_APPS also can collect the static files in core folder to the root Django Project folder with python manage.py collectstatic. I recommend to set core to INSTALLED_APPS before you start building your Django Project:

# "core/settings.py"

INSTALLED_APPS = [
    # ...
    'core', # Here
    'app1',
    'app2',
]

Then, create uppercase and lowercase tags in custom_tags.py as shown below. *You can see my answer explaining @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()

Then, you need to set the file custom_tags by omitting .py in load tag to use uppercase and lowercase tags as shown below:

# "templates/index.html"

{% load custom_tags %}

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

And, you can also create the folders like app1 and app2 with __init__.py(Empty file) and custom_tags.py in templatetags folder as shown below. *Other names are fine for app1 and app2 folders and according to my experiments, custom_tags.py in app1 and app2 folders doesn't work without __init__.py(Empty file) so __init__.py(Empty file) must be created just under app1 and app2 folders:

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

Then, you need to set the file custom_tags following the folders app1 and app2 in load tag to use the tags defined in app1/custom_tags.py and app2/custom_tags.py as shown below:

# "templates/index.html"
                   #  ↓ ↓ Here ↓ ↓     ↓ ↓ Here ↓ ↓
{% load custom_tags app1.custom_tags app2.custom_tags %}

# ...

And, if there are the same tags like uppercase through multiple files as shown below:

# "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"

Then, the uppercase tag of the last file set in load tag is used

# "templates/index.html"
                                    #  ↓ ↓ Here ↓ ↓
{% load custom_tags app1.custom_tags app2.custom_tags %}

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

Lastly, you can create multiple templatetags folders through multiple app folders app1 and app2 in addition to core folder as shown below:

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

However, I recommend to create only one templatetags folder in core folder because there are 3 reasons as shown below:

  1. It is easier to manage only one templatetags folder in core folder than managing multiple templatetags folders through multiple app folders.

  2. For Django Admin, there is no folder to create templatetags folder so core folder is the most reasonable place to do it for Django Admin.

And, the 3rd reason is because through multiple templatetags folders, if there are multiple same name files like custom_tags.py as shown below:

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

Then, only one file is loaded in Django Templates but I don't know which file to be loaded according to my experiments as shown below:

# "templates/index.html"
                             
{% load custom_tags %} # I don't know which `custom_tags.py` is loaded. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文