123

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?

Sliljedal
  • 1,231
  • 2
  • 8
  • 3

19 Answers19

208

Besides putting my_templatetag.py inside app_name/templatetags, make sure you restart the Django development server (or ensure it restarted itself) every time you modify template tags. If the server does not restart, Django won't register the tags.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
110

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',
            
            }
    },
},

]

If you have templatetag dir in your project dir (not in an app dir), then above method is recommended.

Example-
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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dat TT
  • 2,850
  • 2
  • 16
  • 18
50

Make sure you are not missing any of the following steps:

  1. Create a folder called "templatetags" at the same level as models.py and views.py in your application folder

  2. Your application must be in the INSTALLED_APPS in settings.py

  3. The templatetags folder must have __init__.py

  4. Restart the django server

Heapify
  • 2,581
  • 17
  • 17
  • 1
    It seems for me that all my apps require an apps.py and __init__.py to contain `default_app_config = 'custom_admin.apps.Config'`. Otherwise Django 1.10 is not loading templatetags. I don't see this explicitly in the docs though. – Chris Sattinger Feb 06 '18 at 16:30
  • Thanks! I had forgotten to add `__ini__.py`, but do you know why it used to work in Python3 but not in Python2? – Farzad Abdolhosseini Apr 05 '18 at 19:19
  • 2
    It's the restart that gets me every time! Thx for reminding me on #4 ^^ – Marc Apr 24 '18 at 16:05
  • Point #4 helped me "Restart the Django server,",so Django can identify the new template tags. – 0xFK Oct 27 '18 at 07:34
  • Forgot to restart. It worked. – JayB Nov 14 '22 at 18:00
27

In my case the problem was, I was using {% load filter_method_name %}

I had to change to {% load filename %}

I then had to restart the server.

Shamsul Arefin
  • 1,771
  • 1
  • 21
  • 21
10

you have to manually stop the development server and start it again, so Django can identify the new template tags

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
anakyou
  • 111
  • 1
  • 2
5

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

Please see: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ if that isn't the case.

pyjavo
  • 1,598
  • 2
  • 23
  • 41
zubhav
  • 1,519
  • 1
  • 13
  • 19
5

I am using Django 1.11, and I was having the same problem. Some of the answers here are right, but some things may be missing. Here is what I did:

Quoting a previous user:

Create a folder called "templatetags" at the same level as models.py and views.py in your application folder

Your application must be in the INSTALLED_APPS in settings.py

The templatetags folder must have init.py

But, before you re-start the Django server, add this to the file that contains the tags:

from django import template
register = template.Library()

Then you can re-start the server.

Roddy P. Carbonell
  • 858
  • 1
  • 11
  • 16
  • This worked for me when I used '__init__.py' instead of 'init.py'. – William Sep 07 '19 at 19:14
  • What is the difference between the two? One is in bold the other is in normal font, can't understand your statement @William – aspiring1 Mar 01 '21 at 13:50
  • It looks like underscores are bolded now in stackoverflow comments :) I wrote __init__.py with two underscores either side of the 'init'. See Chris Shaw's answer as an example. – William Mar 01 '21 at 17:37
4

Restart the django server. It worked for me after setting the templatetag folder within the app and template_name.py in the templatetag folder.

3

In case it helps someone, the issue in my case was that I was using quotes when trying to load the tag(s)

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

instead of

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

Note: extends needs quotes around the filename but not load

Anupam
  • 14,950
  • 19
  • 67
  • 94
1

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.

Simply copying the __init__.py from another location into the new templatetag's directory sorted it out.

pyjavo
  • 1,598
  • 2
  • 23
  • 41
Chris Shaw
  • 89
  • 4
1

I solved this by adding a templatestag folder in the root with a filter.py file defining my filters, then I adjusted my settings.py.

Please check my complete answer regarding this issue in this similar thread

LucianoBAF
  • 189
  • 2
  • 6
0

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

from app_name.templatetags import my_templatetag

  • inside templatetags folder crate blank file with name __init__.py – Kiran Bachhav. Dec 12 '17 at 12:02
  • 2
    Please [edit your answer](https://stackoverflow.com/posts/47772135/edit) to put all details into it and format it to be readable (use code formatting where appropriate). – Melebius Dec 12 '17 at 14:56
0

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.

Rahul Verma
  • 404
  • 8
  • 20
0

The templatetags folder must have a __init__.py file in order to be a regular python package

Ensure that you also created the templatetags.py file next to the __init__.py file

He3lixxx
  • 3,263
  • 1
  • 12
  • 31
0

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',
            }
        },
    },
]
Bercove
  • 987
  • 10
  • 18
0

This Solved My Problem:

  1. make sure templatetags folder exsist in main django project
  2. use this line in INSTALLED_APPS in project settings.py:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     ...
  
    'my_app.apps.AppConfig',
    
]
0

I know this is old, but in your case you should try {% load has_group from my_templatetag %}

-1

at first stop the server.remove/cut the code from templatetags/tag.py and rewrite/paste it.then run the server.it worked for me

shomit
  • 1
  • 2
    How will cutting, then pasting back in the code help? If you see an error in the question's sample code, please point it out. – Ben Jun 15 '20 at 18:25
-2

Yah, This problem you are currently facing because of older django version Or Complexly to write "Depreciation"

if You HAve These Types OF TAgs In Your template/ Html Files Change Them With..

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

change with


{% load static %}

Get to The Point.. JUst SImply Perform These Replace All These from YOur BAse.html/or Any type Of HTML

Ash
  • 1
  • 1
  • 1
    Can you correct this messy description? It's hard for me to understand your explanation. Please also correct the code formatting. See [answer] – Gander Dec 16 '20 at 09:17