Django 1.10.1 'my_templatetag' is not a registered tag library. Must be one of:

邮差的信 提交于 2019-11-30 02:38:37

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.

Dat TT

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

            }
    },
},

]

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

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

zubhav

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.

Chris Shaw

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.

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.

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!