I want a menu thats custom depending which group you are member of. Im using Django 1.10.1, allauth and so on. When im trying to make my templatetag it fails and it says:¨
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 %}
I have tried to restart the server like millions of times, also i tried to change all the names, and the app is a part of INSTALLED_APPS in settings.py. What am I doing wrong?
you just cut/remove your code which written inside the (example templatetags/home.py) from home.py you remove your code and restart your server and again paste your code in home.py it will work.
put my_templatetag.py inside app_name/templatetags then create init.py inside app_name/templatetags .. Then open terminal in project folder give command python manage.py shell
In case it helps someone, the issue in my case was that I was using quotes when trying to load the tag(s)
instead of
Note:
extends
needs quotes around the filename but notload
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/
Simply copying the __init__.py from another location into the new templatetag's directory sorted it out.
From django 1.9, you can load those new tags/filters in settings like this:
]
Restart the django server. It worked for me after setting the templatetag folder within the app and template_name.py in the templatetag folder.