index.html有一个块,其中包含crudapp app.Index.html中名为init的视图中的上下文。还有一个名为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}
另外,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 %}
我已重启dev服务器。我得到的错误是,
指定的模板库无效。尝试时引发了ImportError load'fileuploader.templatetags.fileuploader_tags':没有命名的模块 模型
并特别提到crudapp / view.py
中的这一行返回渲染(请求,'forum.html',上下文)
并且特定于名为'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)
我之前已成功使用了包含标记,但无法在此处使用它。 任何帮助将不胜感激,
由于
答案 0 :(得分: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