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

标签 django templates inclusion

index.html 有一个 block ,其中包含来自 crudapp 应用程序中名为 init 的 View 的上下文。Index.html 也有一个名为 sidebar_files 的 block ,我尝试使用包含标记填充它。
我在 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}

此外,该目录确实包含一个空的 英迪 .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 中的一个 block 。这是“初始化” View ,
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.pytemplatetags/ 位于同一目录中:
from ..models import FileModel

关于django - 包含标记错误.....指定的模板库无效。尝试加载时引发 ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37773969/

相关文章:

c++ - C++中不带virtual的多态实现多级继承

c++ - 无法推断间接调用模板函数的类型

c++ - 派生为模板参数的基本转换

c++ - Unresolved inclusion 在任何包含文件中

django - csrf_token cookie 被另一个站点删除了?

python - 使用 Django : How to serve a file for download?

python - JSONField 对于 POST 序列化为 json,对于 GET 序列化为字符串

Python - 在 1and1 主机上安装 Pillow 和 MySQL-python

list - 如何检查以下所有项目是否都在列表中?

c++ - 包含守卫的问题