【问题标题】:How can I load custom Django tags from a file?如何从文件中加载自定义 Django 标签?
【发布时间】:2012-03-13 19:32:08
【问题描述】:

我在 mytag.py 文件中有一个 mytag 的标签定义。当我使用带有 settings.py 和 INSTALLED_APPS 的 Django 项目时,这很好用——我将“myapp”附加到列表并将 mytag.py 放在 myapp/templatetags 中。

现在我正在使用 django.conf.settings.configure 并且我没有带有 templatetags 子目录的模块 -- 如何加载 mytag.py?


更新:

我现在尝试使用带有 templatetags 目录的模块,但我无法让它工作。这是我的设置:

文件:

  • 我的应用程序
    • __init.py__
    • 模板标签
      • __init__.py
      • mytag.py
  • 程序
    • test.py
    • test.html

这是相关代码:

# test.py

from django.template import Template, Context
from django.conf import settings

from mostlystatic.processors.mostlystaticprocessor import MostlystaticProcessor
from mostlystatic.stuff import DebugLogger

import sys
sys.path.append("~/")

settings.configure(
    DEBUG=True,
    TEMPLATE_DEBUG = True,
    INSTALLED_APPS = ("myapp",)
    )

t = Template(open("test.html").read())
c = Context({})
content = t.render(c)

print content

# test.html

{% load mytag %}

# mytag.py (doesn't load)

from classytags.arguments import Argument
from classytags.core import Tag, Options
from django import template

register = template.Library()

class MyTag(Tag):
    name="mytag"

    def render_tag(self, context:
        return "test"

register.tag(MyTag)

当我运行 test.py 时,我收到以下消息:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    t = Template(open("test.html").read())
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 125, in __init__
    self.nodelist = compile_string(template_string, origin)
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 153, in compile_string
    return parser.parse()
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 270, in parse
    compiled_result = compile_func(self, token)
  File "/Library/Python/2.7/site-packages/django/template/defaulttags.py", line 1033, in load
    (taglib, e))
django.template.base.TemplateSyntaxError: 'mytag' is not a valid tag library: Template library mytag not found, tried django.templatetags.mytag

【问题讨论】:

  • 您想在不再存在的应用中包含您的标签?
  • “我曾经有,现在我想做,我该怎么做?”跨度>
  • @arie 我还有标签文件,我只是用这个应用来测试它。
  • @Daniel 我希望可能有一种独立的方式来做到这一点,就像设置的其他部分一样。

标签: django django-templates django-custom-tags


【解决方案1】:

最简单的做法是创建一个带有templatetags 子目录的模块,并在配置设置时将该模块包含在INSTALLED_APPS 中。

任何其他方法都将涉及与 Django 内部的角力。

如果您的脚本不起作用,请尝试将其简化为如下所示的非常简单的内容,然后重新构建它。

from django.conf import settings
settings.configure(INSTALLED_APPS=('my_app',))

from django.template import Template
# my_tags does not exist, but the error shows that it is 
# searching my_app's template tag directory
Template.render("{% load my_tags %}")
TemplateSyntaxError: 'my_tags' is not a valid tag library: Template library my_tags not found, tried django.templatetags.my_tags, my_app.templatetags.my_tags

【讨论】:

  • 谢谢,我会试试的。这并不理想,但我认为可以。
  • 我试过了,但还是不行。你知道为什么吗?
  • 我看不到问题,但我可以得到一个简单的例子来工作,我已经添加到我的答案中。从它开始,然后建立它。
  • 您确定它有效吗?它说它找不到图书馆。当我将 my_app 重命名为随机的 - 一个无法搜索的目录 - 我仍然得到相同的结果。
  • 是的,它正在工作。关键是它在寻找my_app.templatetags.my_tags。在您的错误消息中,它只查找django.templatetags.mytag,这表明 my_app 不是已安装的应用程序。
猜你喜欢
  • 2016-02-25
  • 2011-07-10
  • 2017-07-29
  • 2010-10-07
  • 2010-11-15
  • 1970-01-01
  • 2019-12-30
  • 2012-07-07
  • 1970-01-01
相关资源
最近更新 更多