在 Django 中使用 templatetags 时导入错误

发布于 2024-08-31 21:34:24 字数 1086 浏览 1 评论 0原文

好吧,当我尝试在 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 文件夹(均在已安装的应用程序和 TEMPLATE_DIRS 中)。 但是当我想在我的html中使用 {% loadclusion_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

Well, when I'm trying to use 'inclusion' in Django, I met some confused problems that I can't solve it by myself.

There is the structures for my project.

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

I have registered templatetags folder in the settings.py (Both in Installed APPS & TEMPLATE_DIRS).
But when I want to use {% load inclusion_test %} in my html, it raise an exception like this:

'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

I think there is nothing wrong with my import work, how can I do with that?

Thanks for help!

My django version: 1.0+
My Python version: 2.6.4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2024-09-07 21:34:24
  1. templatetags 文件夹应该位于应用程序中文件夹

    <前><代码> App1---
    __init__.py
    模型.py
    测试.py
    urls.py
    视图.py
    模板标签---
    __init__.py
    包含_测试.py
    ...

  2. 您注册了该标签吗?

例子:

register = template.Library()   
@register.inclusion_tag('platform/templatetags/pagination_links.html')
def pagination_links(page, per_page, link):
  1. The templatetags folder should live in the app folder:

            App1---
                   __init__.py
                   models.py
                   test.py
                   urls.py
                   views.py
                   templatetags---
                           __init__.py
                           inclusion_test.py
                              ...
    
  2. Did you registered the tag?

Example:

register = template.Library()   
@register.inclusion_tag('platform/templatetags/pagination_links.html')
def pagination_links(page, per_page, link):
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文