mytag.pyファイルにmytagのタグ定義があります。これは、settings.pyとINSTALLED_APPSでDjangoプロジェクトを使用しているときにうまく動作します。 - myappをリストに追加し、mytag.pyをmyapp/templatetagsに追加します。ファイルからカスタムDjangoタグを読み込むにはどうしたらいいですか?
私はdjango.conf.settings.configureを使用していますが、templatetagsサブディレクトリのモジュールはありません。mytag.pyをロードするにはどうしたらいいですか?
更新:
私は今templatetagsディレクトリにモジュールを使用しようとしましたが、私はそれを動作させることはできません。
ファイル:ここに私のセットアップです
- myappの
__init.py__
- templatetags
__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
"ドキュメンテーションに従ったものを持っていましたが、今は<プロジェクトの設定方法を完全に破壊するもの>をしたいのですが、どうすればいいですか? –
@arie私はまだタグファイルを持っています、私はそれをテストするためにアプリを使いました。 – fgm2r