Django: 'current_tags' is not a valid tag library

前端 未结 17 2689
悲&欢浪女
悲&欢浪女 2020-12-23 18:41

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 serv

相关标签:
17条回答
  • 2020-12-23 19:06

    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 讨论(0)
  • 2020-12-23 19:08

    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/

    0 讨论(0)
  • 2020-12-23 19:08

    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.

    0 讨论(0)
  • 2020-12-23 19:08

    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.

    0 讨论(0)
  • 2020-12-23 19:10

    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.

    0 讨论(0)
  • 2020-12-23 19:14

    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.

    0 讨论(0)
提交回复
热议问题