2016-06-12 41 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 
相關問題