Djangoのテンプレート内で自作の関数を呼び出したいと思い、
カスタムテンプレートを作成しましたが、そもそも読み込まれない状態でつまづいております。
知見のある方、解決策をご教示いただけると幸いです。
環境
Django 2.1.7
macOS 10.14.4
Pycharm
エラー内容
InvalidTemplateLibrary at / Invalid template library specified. ImportError raised when trying to load 'project.templatetags.utility': No module named 'project.templatetags'
構造
アプリ(contents)とtemplateの同じ階層にtemplatetagsフォルダを作成。
templatetagsフォルダ直下にinit.pyとutility.pyを作成。
utility.py
python
1from django import template 2 3register = template.Library() 4 5 6@register.filter() 7def display_sample(value): 8 return True
setting.py
python
1# Application definition 2 3INSTALLED_APPS = [ 4 'django.contrib.admin', 5 'django.contrib.auth', 6 'django.contrib.contenttypes', 7 'django.contrib.sessions', 8 'django.contrib.messages', 9 'django.contrib.staticfiles', 10 'contents.apps.ContentsConfig', 11 'widget_tweaks', 12 'templatetags.utility', 13] 14 15TEMPLATES = [ 16 { 17 'BACKEND': 'django.template.backends.django.DjangoTemplates', 18 'DIRS': [os.path.join(BASE_DIR, 'templates')] 19 , 20 'APP_DIRS': True, 21 'OPTIONS': { 22 'context_processors': [ 23 'django.template.context_processors.debug', 24 'django.template.context_processors.request', 25 'django.contrib.auth.context_processors.auth', 26 'django.contrib.messages.context_processors.messages', 27 ], 28 'libraries': { 29 'utility': 'project.templatetags.utility', 30 } 31 }, 32 }, 33]
base.html
{% load static %} {% load utility %}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/27 14:32
2019/04/29 00:25