Django 1.10.1 'my_templatetag' 不是注册标签库。必须是 : 之一

标签 django django-allauth templatetags

我想要一个自定义菜单,具体取决于您所属的组。 我正在使用 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' 看起来像这样:

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()

然后错误出现在我的 .html 文件中,它说,

{%  load my_templatetag %}

我已经尝试过数百万次重启服务器,我还尝试更改所有名称,并且该应用程序是 settings.py 中 INSTALLED_APPS 的一部分。 我做错了什么?

最佳答案

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

关于Django 1.10.1 'my_templatetag' 不是注册标签库。必须是 : 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40686201/

相关文章:

jquery - 将类添加到 django 表单小部件字段

django-allauth:如何正确使用 email_confirmed 信号将用户设置为事件状态

python - Django TemplateTags 不编译

html - 用于渲染 html 子集的 Django templatetag

Django 模板标签范围迫使我做额外的查询

python - LiveServerTestCase 服务器看到不同的数据库进行测试

python - 在 django 自定义模型管理添加页面处理自定义提交按钮的正确方法

python - Django 代理字段

django - 使用 Django-AllAuth 以编程方式登录?

Django 全授权 : Doesn't show First name and Last name while signing up for new accout