【问题标题】:Inclusion tag error ..... Invalid template library specified. ImportError raised when trying to load包含标记错误.....指定的模板库无效。尝试加载时引发 ImportError
【发布时间】:2016-10-12 22:34:10
【问题描述】:

index.html 有一个块,其中包含来自 crudapp app 中名为 init 的视图的上下文。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 %}

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

指定的模板库无效。尝试时引发 ImportError 加载'fileuploader.templatetags.fileuploader_tags':没有命名的模块 型号

并特别提到了 crudapp/view.py 中的这一行

返回渲染(请求,'forum.html',上下文)

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

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)

我曾经成功地使用过包含标签,但在这里无法使用它。 任何帮助将不胜感激,

谢谢

【问题讨论】:

    标签: django templates inclusion


    【解决方案1】:

    正如错误提示的那样,Python 找不到 models 模块。问题是您的fileuploader_tags.py 中的这一行:

    from .models import FileModel
    

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

    from fileuploader.models import FileModel
    

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

    或者,使用相对路径,假设models.pytemplatetags/ 位于同一目录中:

    from ..models import FileModel
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2021-12-23
      • 1970-01-01
      • 2022-11-03
      • 2012-05-09
      • 2019-09-13
      相关资源
      最近更新 更多