Django: 'current_tags' is not a valid tag library

PythonDjangoPortability

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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/

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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

Solution 5 - Python

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.

Solution 6 - Python

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

Solution 7 - Python

"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

Solution 8 - Python

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.

Solution 9 - Python

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

Solution 10 - Python

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.

Solution 11 - Python

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.

Solution 12 - Python

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.

Solution 13 - Python

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.

Solution 14 - Python

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.

Solution 15 - Python

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

Solution 16 - Python

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

I had to change to {% filename %}

Solution 17 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsnakileView Question on Stackoverflow
Solution 1 - PythonmifView Answer on Stackoverflow
Solution 2 - Pythonty.View Answer on Stackoverflow
Solution 3 - PythonCtrl-CView Answer on Stackoverflow
Solution 4 - PythonKrishnadas PCView Answer on Stackoverflow
Solution 5 - Pythonsteven2308View Answer on Stackoverflow
Solution 6 - PythonMuhammad SolimanView Answer on Stackoverflow
Solution 7 - PythonPawan KumarView Answer on Stackoverflow
Solution 8 - PythonAmiNadimiView Answer on Stackoverflow
Solution 9 - PythonMohamedView Answer on Stackoverflow
Solution 10 - PythonmanuView Answer on Stackoverflow
Solution 11 - PythonFlimmView Answer on Stackoverflow
Solution 12 - PythonLiyosiView Answer on Stackoverflow
Solution 13 - PythonEran GoldinView Answer on Stackoverflow
Solution 14 - PythonDennis GolomazovView Answer on Stackoverflow
Solution 15 - PythondhanaView Answer on Stackoverflow
Solution 16 - PythonShamsul Arefin SajibView Answer on Stackoverflow
Solution 17 - PythonRadekView Answer on Stackoverflow