【问题标题】:Import Error when use templatetags in Django在 Django 中使用模板标签时导入错误
【发布时间】:2010-05-05 09:14:35
【问题描述】:

好吧,当我尝试在 Django 中使用“包含”时,我遇到了一些我无法自己解决的困惑问题。

我的项目有结构。

MyProject---
            App1---
                   __init__.py
                   models.py
                   test.py
                   urls.py
                   views.py
            App2---
                   ...
            template---
                       App1---
                              some htmls
                       App2---
                              ...
            templatetags---
                           __init__.py
                           inclusion_cld_tags.py
            manage.py
            urls.py
            __init__.py
            settings.py

我在 settings.py 中注册了 templatetags 文件夹(都在 Installed APPS 和 TEMPLATE_DIRS 中)。 但是当我想在我的 html 中使用 {% load contains_test %} 时,它会引发如下异常:

'inclusion_cld_tags' is not a valid tag library: Could not load template library from django.templatetags.inclusion_cld_tags, No module named inclusion_cld_tags

我认为我的导入工作没有问题,我该怎么办?

感谢您的帮助!

我的 django 版本:1.0+ 我的 Python 版本:2.6.4

【问题讨论】:

    标签: python django templatetags


    【解决方案1】:
    1. templatetags 文件夹should live in the app folder:

              App1---
                     __init__.py
                     models.py
                     test.py
                     urls.py
                     views.py
                     templatetags---
                             __init__.py
                             inclusion_test.py
                                ...
      
    2. 你注册了标签吗?

    例子:

    register = template.Library()   
    @register.inclusion_tag('platform/templatetags/pagination_links.html')
    def pagination_links(page, per_page, link):
    

    【讨论】:

    • 感谢您的回答 1.我尝试将 templatetags 文件夹放入我的一个应用程序中,但它也不起作用。 2.我已经像上面给出的例子一样注册了它,在问这个问题之前做'from django import template'。
    • 哦...嗯...好吧我终于解决了。 =.= 你是对的,templatetags 文件夹应该位于 app 文件夹中,之后我应该在 settings.py 中更改 INSTALLED_APPS 的设置,以便它可以正确放置在我的项目中。毕竟,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-03-04
    • 2018-07-31
    • 2014-03-20
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多