index.html 有一个块,其中包含来自 crudapp 应用程序中名为 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 %}
我已经重新启动了开发服务器。我得到的错误是,
指定的模板库无效。尝试加载“fileuploader.templatetags.fileuploader_tags”时引发 ImportError:没有名为模型的模块
并特别提到了 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)
我曾经成功地使用过包含标签,但无法让它在这里工作。任何帮助将不胜感激,
谢谢