包含标签错误..... 指定的模板库无效。尝试加载时引发 ImportError

Inclusion tag error ..... Invalid template library specified. ImportError raised when trying to load

index.html 有一个包含来自 crudapp 中名为 init 的视图的上下文的块 app.Index.html 也有一个名为 sidebar_files 的块,我试图使用包含标记填充它。 我在 fileuploader/templatetags/fileuploader_tags.py,

中创建了一个包含标签
from django.db import models
from .models import FileModel
from django import template
register = template.Library()

@register.inclusion_tag('sidebar_files.html')
def file_sharing_sidebar():
    file_model = FileModel.objects.all().reverse()
    return {'file_model': file_model}

此外,该目录确实包含一个空的 inti.py 文件(使用双下划线)。 在项目模板文件中,我有 sidebar_files.html 和加载标签,

{% load fileuploader_tags %}
{% block sidebar %}
    {% for item in file_model %}
      .... blah .....
    {% endfor %}
  {% endif %}
</div>
{% endblock %}

该应用包含在 INSTALLED_APPS 中。 我的主 index.html 文件使用 fileuploader_tag 并尝试使用 sidebar_file.html 模板,

{% load staticfiles %}
{% load fileuploader_tags %}
......
{% block sidebar %} {% file_sharing_sidebar %}{% endblock %}

我已经重启了开发服务器。我得到的错误是,

Invalid template library specified. ImportError raised when trying to load 'fileuploader.templatetags.fileuploader_tags': No module named models

并特别提到 crudapp/view.py

中的这一行

return render(request, 'forum.html', context)

并且特定于名为 'init' 的主视图,它将其上下文发送到 forum.html。此模板是 index.html 中的一个块。这是 'init' 视图,

def init(request):
    postModel = list(PostModel.objects.raw('SELECT *, max(pub_date), count(topic_id) AS freq, count(DISTINCT author) AS contributors FROM crudapp_postmodel GROUP BY topic_id ORDER BY pub_date DESC'))
    paginator = Paginator(postModel, 8)
    page2 = request.GET.get('page')
    try:
        forum_model = paginator.page(page2)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        forum_model = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        forum_model = paginator.page(paginator.num_pages)

    blogModel = BlogModel.objects.all().order_by('pub_date').reverse()
    paginator = Paginator(blogModel, 5)
    page = request.GET.get('blog')
    try:
        blog_model = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        blog_model = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        blog_model = paginator.page(paginator.num_pages)
    # context = {'blog_model': blog_model}

    totalposts = PostModel.objects.annotate(Count('post'))
    totalusers = User.objects.annotate(Count('id'))
    totalfiles = FileModel.objects.filter(approved=True).annotate(Count('upload'))
    totalarticles = BlogModel.objects.filter(approved=True).annotate(Count('article'))
    totalviews = TopicModel.objects.aggregate(numviews = Sum('views'))
    # If there are topis with no posts the number of topics below will still be correct
    totaltopics = PostModel.objects.aggregate(numtopics = Count('topic__id', distinct=True))
    context = {'blog_model': blog_model, 'forum_model': forum_model, 'current_time':   timezone.now(), 'totalarticles': totalarticles, 'totalfiles': totalfiles, 'totalposts': totalposts, 'totaltopics': totaltopics, 'totalusers': totalusers, 'totalviews': totalviews}
    return render(request, 'forum.html', context)

我以前成功使用过一次包含标签,但在这里无法使用。 任何帮助将不胜感激,

谢谢

如错误所示,Python 找不到 models 模块。问题是您 fileuploader_tags.py:

中的这一行
from .models import FileModel

它将尝试在与当前文件相同的目录中寻找models.py 。将其更改为:

from fileuploader.models import FileModel

(假设您的应用名为 fileuploader)。

或者,要使用相对路径,假设 models.py 位于与 templatetags/ 相同的目录中:

from ..models import FileModel