Django 1.10.1“ my_templatetag”不是注册的标记库。必须是以下内容之一:

我想要一个自定义的菜单,取决于你是哪个组的成员。 我使用 Django 1.10.1,allauth 等等。 当我试图让我的模板标签失败,它说:

TemplateSyntaxError at /
'my_templatetag' is not a registered tag library. Must be one of:
account
account_tags
admin_list
admin_modify
admin_static
admin_urls
cache
i18n
l10n
log
socialaccount
socialaccount_tags
static
staticfiles
tz

'my_templatetag.py' looks like this:

from django import template
from django.contrib.auth.models import Group




register = template.Library()


@register.filter(name='has_group')
def has_group(user, group_name):
group =  Group.objects.get(name=group_name)
return group in user.groups.all()

and tha error comes in my .html file which say,

{%  load my_templatetag %}

我已经尝试重新启动服务器好几百万次了,我还尝试更改所有的名称,这个应用程序是 setings.py 中 INSTALLED _ APPS 的一部分。 我做错了什么?

113444 次浏览

Where is 'my_templatetag.py' stored? It should be stored in a directory called 'templatetags' which is within the app.

请参阅: < a href = “ https://docs.djangoproject.com/en/dev/howto/custom- template-tag/”rel = “ nofollow norefrer”> https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ if that isn't the case.

除了将 my_templatetag.py放入 app_name/templatetags之外,还要确保每次修改模板标记时都重新启动 Django 开发服务器(或确保它自己重新启动)。如果服务器不重新启动,Django 将不会注册标记。

From Django 1.9, you can load those new tags/filters in settings like this:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
'app.apptemplates.load_setting',
            

],
                

'libraries':{
'my_templatetag': 'app.templatetags.my_templatetag',
            

}
},
},

]

如果您的项目目录中有 templatetag 目录(不在应用程序目录中) ,那么建议使用上述方法。

例子-
enter image description here

Quoting: Https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/#:~:text=it%20also%20enables%20you%20to%20register%20tags%20without%20installing%20an%20application.

I know this is a bit old, but I ran into the same problem today. I found the solution in the docs: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

The app should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it - don’t forget the __init__.py file to ensure the directory is treated as a Python package.

只需将 _ _ init _ _. py 从另一个位置复制到新的 templatetag 的目录中即可。

重新启动 django 服务器。在应用程序中设置 templatetag 文件夹并在 templatetag 文件夹中设置 template _ name.py 之后,我就可以使用它了。

确保你没有错过以下任何一个步骤:

  1. 创建一个名为“ templatetags”的文件夹,该文件夹的级别与 mods.py 相同 以及应用程序文件夹

  2. 中的 views.py
  3. 您的应用程序必须位于 setings.py 中的 INSTALLED _ APPS 中

  4. Templatetags 文件夹必须具有 _ _ init _ _. py

  5. 重新启动 django 服务器

将 my _ templatetag.py 放在 app _ name/templatetags 中,然后在 app _ name/templatetags 中创建 Init.py。.然后打开项目文件夹中的终端,给命令 python management. py shell

从 app _ name. templatetags 导入 my _ templatetag

你只需要剪切/删除写在(例如 templatetags/home.py)中的代码 从 home.py 中删除代码并重启服务器,然后再次将代码粘贴到 home.py 中,这样就可以工作了。

我正在使用 Django 1.11,我也遇到了同样的问题。这里的一些答案是正确的,但有些东西可能缺失。我是这么做的:

引用一位前用户的话:

Create a folder called "templatetags" at the same level as models.py 以及应用程序文件夹中的 views.py

您的应用程序必须位于 setings.py 中的 INSTALLED _ APPS 中

模板标签文件夹必须具有 init.py

但是,在重新启动 Django 服务器之前,请将此添加到包含标记的文件中:

from django import template
register = template.Library()

然后您可以重新启动服务器。

在我的案例中,问题是,我使用的是 {% load filter_method_name %}

我不得不换成 {% load filename %}

然后我不得不 重新开始服务器。

如果对某些人有帮助的话,我的例子中的问题是在试图加载标记时使用了引号

{%  load 'my_templatetag' %}  <!-- incorrect -->

而不是

{%  load my_templatetag %}  <!-- correct -->

注意: extends需要引号围绕文件名,而不是 load

首先停止 server.move/cut templatetags/tag.py 中的代码并重写/粘贴. 然后运行 server.it

您必须手动停止开发服务器并重新启动它, 这样 Django 就可以识别新的模板标记

是啊,你现在面临的这个问题,因为老姜戈版本或复杂写“折旧”

如果您的模板/HTML 文件中有这些类型的标记,请使用。

> \{\{% load staticfiles %} or  {% load admin_static %}, {% load
> admin_static %}}

改变


{% load static %}

说重点。 只要简单地执行这些代替所有这些来自您的 BASE. HTML/或任何类型的 HTML

我通过在根目录中添加一个 templatestag文件夹来解决这个问题,这个 Filter py文件定义了我的过滤器,然后我调整了我的 设置

请在 这条相似的线中检查我关于这个问题的完整答案

The templatetags folder must have a __init__.py file in order to be a 普通的 python 软件包

确保您还创建了 _ _ init _ _. py 文件旁边的 templatetags.py 文件

For me, I had to register my customer Filter as this since my templatetags are outside of any app

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
'django.template.context_processors.media',
],
# ! New Line
'libraries':{
'customFilter': 'templatetags.customFilter',
}
},
},
]