Inclusion tag error ..... Invalid template library specified. ImportError raised when trying to load
As the error suggests, Python is failing to find a models
module. The problem is this line in your fileuploader_tags.py
:
from .models import FileModel
Which will try to look for a models.py
in the same directory as the current file. Change it to:
from fileuploader.models import FileModel
(This assumes your app is called fileuploader
).
or, to use a relative path, assuming models.py
is located in the same directory as templatetags/
:
from ..models import FileModel
Shane G
Updated on June 17, 2022Comments
-
Shane G almost 2 years
index.html has a block containing the context from a view called init in the crudapp app.Index.html also has a block called sidebar_files which I trying to populate using an inclusion tag. I have created an inclusion tag in 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}
Also that directiory does contain an empty inti.py file (using double underscores). In the project template files I have the sidebar_files.html with the load tags,
{% load fileuploader_tags %} {% block sidebar %} {% for item in file_model %} .... blah ..... {% endfor %} {% endif %} </div> {% endblock %}
The app is included in INSTALLED_APPS. My main index.html file used the fileuploader_tag and tries to use the sidebar_file.html template like this,
{% load staticfiles %} {% load fileuploader_tags %} ...... {% block sidebar %} {% file_sharing_sidebar %}{% endblock %}
I have restarted the dev server. The error I get is,
Invalid template library specified. ImportError raised when trying to load 'fileuploader.templatetags.fileuploader_tags': No module named models
and specifically mentions this line from crudapp/view.py
return render(request, 'forum.html', context)
and is specific to the main view called 'init' which sends its context to forum.html. This template is a block in index.html. Here is the 'init' view,
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)
I have used the inclusion tag before once, successfully, but can't get it to work here. Any help would be greatly appreciated,
Thanks