I've built a menu
as a templatetag
in Django>1.9. The problem is that following this solution, I can't put the templatetag at the root of the folder, as I'm getting a:
我在Django> 1.9中构建了一个菜单作为模板标签。问题是,遵循这个解决方案,我不能把模板标签放在文件夹的根目录,因为我得到一个:
TemplateSyntaxError: 'menu' is not a registered tag library
TemplateSyntaxError:'menu'不是已注册的标记库
Below is the part of my settings.py
that I've modified:
以下是我修改过的settings.py的一部分:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# Look for base template at root of project
'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
# Look for base templatetags at root of project
'libraries': {
'project_tags': 'templatetags.menu',
}
},
},
]
Even with an empty menu.py
template tag like the one below I get the same error:
即使有一个像下面那样的空menu.py模板标签,我也会得到同样的错误:
from django import template
register = template.Library()
@register.inclusion_tag('menu.html', takes_context=True)
def menu(context):
pass
Does Django
support project-wide templatetags
at all?
Django是否支持项目范围的模板标签?
1 个解决方案
#1
0
I've had it fixed by loading in my base.html
template the module (i.e. project_tags
) instead of the tag (i.e. menu
), and then calling normally the tag menu
:
我通过在我的base.html模板中加载模块(即project_tags)而不是标签(即菜单)来修复它,然后正常调用标签菜单:
So it's
{% load project_tags %}
...
{% menu %}
Instead of:
{% load menu %}
...
{% menu %}
#1
0
I've had it fixed by loading in my base.html
template the module (i.e. project_tags
) instead of the tag (i.e. menu
), and then calling normally the tag menu
:
我通过在我的base.html模板中加载模块(即project_tags)而不是标签(即菜单)来修复它,然后正常调用标签菜单:
So it's
{% load project_tags %}
...
{% menu %}
Instead of:
{% load menu %}
...
{% menu %}