88👍
I would suggest the following:
-
(Most likely) You haven’t installed one of the dependencies of your tag library. Check the imports inside the
current_tags.py
module. -
Make sure the application that includes the tag library is registered in
settings.py
underINSTALLED_APPS
. -
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👍
I had this problem and fixed it by adding a blank __init__.py
file in my appname/templatetags/ directory.
- [Django]-Resize fields in Django Admin
- [Django]-How to create a Django FloatField with maximum and minimum limits?
- [Django]-Define css class in django Forms
64👍
Possibilities are many:
- You haven’t reset your dev server.
- You have dependency loop in templatetag file.
- You misspelled something (directory, folder, template name in ‘load’, etc.).
- You forgot about adding the app to INSTALLED_APPS.
- [Django]-How do i pass GET parameters using django urlresolvers reverse
- [Django]-Running a specific test case in Django when your app has a tests directory
- [Django]-What is reverse()?
13👍
Restart the server has solved the issue for me. They must have mentioned it in the documentation.
- [Django]-Add additional options to Django form select widget
- [Django]-How to strip html/javascript from text input in django
- [Django]-Django rest framework lookup_field through OneToOneField
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
.
- [Django]-Define css class in django Forms
- [Django]-Django-tables2: How to use accessor to bring in foreign columns?
- [Django]-How do I create a slug in Django?
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
- [Django]-What's the difference between `from django.conf import settings` and `import settings` in a Django project
- [Django]-How to make two django projects share the same database
- [Django]-Django access the length of a list within a template
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
- [Django]-History of Django's popularity
- [Django]-Why is __init__ module in django project loaded twice
- [Django]-How do you insert a template into another template?
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.
- [Django]-Django Admin – Disable the 'Add' action for a specific model
- [Django]-Get list item dynamically in django templates
- [Django]-Django: Redirect to previous page after login
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…
- [Django]-Django: Get an object form the DB, or 'None' if nothing matches
- [Django]-Django: reverse accessors for foreign keys clashing
- [Django]-Django: How do I add arbitrary html attributes to input fields on a form?
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.
- [Django]-When saving, how can you check if a field has changed?
- [Django]-Tailwindcss: fixed/sticky footer on the bottom
- [Django]-Difference between auto_now and auto_now_add
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.
- [Django]-Django JSONField inside ArrayField
- [Django]-Django-Forms with json fields
- [Django]-How to get the username of the logged-in user in Django?
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.
- [Django]-How to delete a record in Django models?
- [Django]-Backwards migration with Django South
- [Django]-Define css class in django Forms
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.
- [Django]-Using {% url ??? %} in django templates
- [Django]-UUID as default value in Django model
- [Django]-Error when using django.template
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.
- [Django]-Dynamically adding a form to a Django formset
- [Django]-Django – Render the <label> of a single form field
- [Django]-Bypass confirmation prompt for pip uninstall
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
- [Django]-Django: save() vs update() to update the database?
- [Django]-Difference between auto_now and auto_now_add
- [Django]-Django: How can I create a multiple select form?
0👍
In my case the problem was,
I was using {% load filter_method_name %}
I had to change to {% filename %}
- [Django]-Django setUpTestData() vs. setUp()
- [Django]-How do you know if memcached is doing anything?
- [Django]-Django: Redirect logged in users from login page
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.
- [Django]-How to stop gunicorn properly
- [Django]-Best way to make Django's login_required the default
- [Django]-Reference list item by index within Django template?