79
votes

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?

17
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
@chucksmash Thank you Jesus. - smilebomb
remember to restart the dev-server - brainLoop

17 Answers

88
votes

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/

78
votes

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

64
votes

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.
12
votes

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

6
votes

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.

6
votes

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

5
votes

"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
4
votes

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.

3
votes

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
votes

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
votes

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
votes

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
votes

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
votes

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
votes

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
votes

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

I had to change to {% filename %}

0
votes

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.