2016-06-12 40 views
2

index.html有一个包含来自称为init的crudapp app.Index.html视图的上下文的块也有一个名为sidebar_files的块,我尝试使用包含标记填充该块。 我已经创造了fileuploader/templatetags/fileuploader_tags.py一个包含标签,包含标记错误.....指定的模板库无效。尝试加载时引发的ImportError

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} 

也是directiory确实包含一个空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 %} 

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

指定了无效的模板库。没有名为 车型

,并特别提到了从crudapp这一行/ view.py

返回渲染(请求,论坛模块:导入错误试图 负载 'fileuploader.templatetags.fileuploader_tags' 时提出.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) 

我已经使用了包含标签前一次成功,但不能让它在这里工作。 任何帮助将不胜感激,

感谢

回答

4

由于错误提示,Python是没有找到一个models模块。问题是,这条线在你的fileuploader_tags.py

from .models import FileModel 

这将尝试寻找一个models.py在同一目录作为当前文件。将其更改为:

from fileuploader.models import FileModel 

(这里假设你的应用程序被称为fileuploader)。

,或者使用相对路径,假定models.py位于同一目录templatetags/

from ..models import FileModel 
相关问题