80

I have a small Django project I received from a friend. The code works perfectly on his system. However, on my system I get the following error message when running the server:

TemplateSyntaxError at /

'current_tags' is not a valid tag library: Template library current_tags not found, tried django.templatetags.current_tags

The problem is with a line in an html file:

{% load current_tags %}

This exact same code works on his system with no errors. What could that be?

3
  • 78
    In the interest of saving others the 20 minutes I spent rooting through these questions: if you've just created your custom tags file (this is your first custom tag in a given app), you'll need to restart the Django development server so that it can pick up the new file. This error is displayed when that is your only problem too.
    – chucksmash
    Apr 7, 2013 at 16:52
  • 2
    @chucksmash Thank you Jesus.
    – smilebomb
    May 11, 2017 at 19:15
  • 2
    remember to restart the dev-server
    – brainLoop
    Oct 15, 2018 at 9:10

17 Answers 17

88

I would suggest the following:

  1. (Most likely) You haven't installed one of the dependencies of your tag library. Check the imports inside the current_tags.py module.

  2. Make sure the application that includes the tag library is registered in settings.py under INSTALLED_APPS.

  3. Make sure that you can successfully import the tag library.

    python manage.py shell
    >>> from app.templatetags import current_tags
    

    This boils down what the following link recommends, which is that the error itself tends to mislead you about where it's looking for a template from. It silently ignores errors on import, which means current_tags.py itself might have a syntax error or another reason why it raises ImportError.

If everything else fails, check this link: http://www.b-list.org/weblog/2007/dec/04/magic-tags/

6
  • 34
    If those don't work, check the order of apps in INSTALLED_APPS. If one of your apps uses a template tag from another app, that second app is by definition a dependency and must be listed before.
    – JCotton
    Nov 11, 2011 at 20:09
  • 9
    If still not working, remove your .pyc files and try again (that solved my problem once)
    – semente
    Apr 15, 2013 at 18:49
  • 3
    Similar to the comment above, I've tried numerous fixes and I didn't notice that other Django console instances were being used, which caused some libraries not to refresh! If you think everything's ok and just can't find the problem, close all running instances and try again.
    – Nimo
    Jan 12, 2014 at 11:56
  • 20
    Don't forget to turn it on and off again ;), meaning, don't forget to restart the server Jun 12, 2014 at 20:32
  • 1
    Maybe this is silly advice but restart your runserver. You get used to it reloading every time you change python code but the reload is either not triggered by adding a new template tag file or it does not register new template tag libraries.
    – nmgeek
    Sep 11, 2016 at 18:22
78

I had this problem and fixed it by adding a blank __init__.py file in my appname/templatetags/ directory.

1
  • 10
    I had to restart dev server. Oct 10, 2016 at 16:51
64

Possibilities are many:

  1. You haven't reset your dev server.
  2. You have dependency loop in templatetag file.
  3. You misspelled something (directory, folder, template name in 'load', etc.).
  4. You forgot about adding the app to INSTALLED_APPS.
0
13

Restart the server has solved the issue for me. They must have mentioned it in the documentation.

6

I was getting the same error but for a different reason so I'll tell you (in case someone else comes the same problem).

I had everything right but I had my custom tag inside a folder named template_tags and after a long search I found out that it had to be templatetags, and now it works. So check the folder name is exactly templatetags.

2
  • Excellent answer. Django's error message is not obvious in this case.
    – martincho
    Mar 14, 2015 at 23:01
  • I was about to add this answer, in case it wasnt here already. Exactly my problem :P Sep 29, 2017 at 16:59
6

suppose you have the following structure:

-- Application_Name

-------templatetags

--------------init.py

--------------templates_extras.py

-------init.py

-------settings.py

-- manage.py

You have to make sure of the following:

  • your application itself inside which your "templatetags" is resident is actually installed in INSTALLED_APPS in settings.py (e.g. "Application_Name")

  • your tag module itself that exists inside "templatetags" is already installed in INSTALLED_APP in settings.py (e.g. "ApplicationName.templatetags.tempaltes_extras")

  • keep sure you have "init.py" under templatetags directory

  • you have to restart the server

  • In some cases you have to remove all generated *.pyc if it did not work then retry again

2
  • your tag module itself that exists inside "templatetags" is already installed in INSTALLED_APP in settings.py (e.g. "ApplicationName.templatetags.templates_extras") This was crucial! You would assume 'ApplicationName' would be enough in settings.py but that is not so. Thank you!
    – cheenbabes
    Feb 12, 2015 at 22:11
  • No module named 'ApplicationName.templatetags.templates_extras"
    – 3pitt
    Jul 1, 2018 at 20:22
5

"custom tags" is not a valid tag library error, more often is occurred because the custom tags are not loaded into the app.

place an empty init.py inside the same folder where your "custom template tag" is placed and run the below code on the terminal to load the custom template tags

touch __init__.py
2
  • Despite having the right directory structure, and having the init.py file already in there... I got this error until I ran this command. Thank you!
    – mungojerie
    Mar 1, 2015 at 23:59
  • I still get a ModuleNotFound error (when i try to restart the server)
    – 3pitt
    Jul 1, 2018 at 20:26
5

For others facing this . Suppose your App name is MyApp and your tag folder name is templatetags then in settings.py you should have :

INSTALLED_APPS = [
'MyApp',
'MyApp.templatetags'
]

Both your django app and your tag folder which is under your app package are needed there.

-> MyApp
    ---> models.py
    ---> views.py
    ---> templatetags
      -----> __init__.py
      -----> app_filters.py

And in your template file :

{% load app_filters %}

Also app_filters.py be like :

# coding=utf-8
from django import template

register = template.Library()


@register.filter(name='get_item')
def get_item(dictionary, key):
    return dictionary.get(key)

check all above steps and you may find the issue.

1
  • 1
    I used to make the mistake to load the specific tag rather than the file containing the tag. Thanks a lot
    – Ramtin
    Sep 28, 2018 at 15:25
3

Please ensure your templatetags folder is initialized with python, if you are in doubt, just take bakup of all files,

Remove all files, Inside templatetags folder create init.py file only, then restart your server,

Now your folder is under Python, then do your stuff.

This works for me...

3

For me, it was the mistake of putting quotes around the library name in load tag.

WRONG: {% load 'library_name' %}

CORRECT: {% load library_name %}

Refer to other answers too. I solved a couple of those problems too before landing here.

2

Make sure the load statement is correct. It should be the name of the file, not the name of the app. For instance, if you have this app:

appname
├── __init__.py
├── templatetags
│   ├── __init__.py
│   └── foobarfilename.py

Then you should put this in your template:

{% load foobarfilename %}

Of course, you should check the other answers too.

2

After you have created the template tag and it should be within the 'templatetags' package within an app installed in the settings.INSTALLED_APPS, make sure you restart your dev-server.

1

Maybe someone will find this useful: somehow I had named the directory 'templatetags ' instead of 'templatetags', that is -- with a space at the end. Took hours to finally realize.

0

All of the advice listed here didn't help me. So in my specific case the problem was that the templatetag had to be loaded from a third-party app, and I manually copied source folder with that app into src folder in my virtualenv. Then I ran python setup.py install inside that folder. After that django could not load this module.

Then I removed the source and installed folder of this app and installed it using pip install -r requirements.txt after adding a relevant line into requirements.txt file. It was downloaded into the src folder, installed and everything began working properly. Hope this helps someone.

0

In my case I have created library instance using tag variable instead of register variable

tag = template.Library()

But it should be

register = template.Library()

To be a valid tag library, the module must contain a module-level variable named register that is a template.Library instance, in which all the tags and filters are registered

0

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

I had to change to {% filename %}

0

In my case it was - I am using

@register.inclusion_tag('template-to-use.html')

I forgot to create that template and right away it started working. I know above answers are more related to most of the issues - but hope maybe someone find it useful. It should have gotten to me:

Template does not exist

but it did not and this worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.