I have created a templatetags folder inside my application and inside a file named posts.py, I have written the following code;
from django.template import Library, Node
from advancedviews.models import Post
register = Library()
class AllPost(Node):
def render(self,context):
context['all_posts'] = Post.objects.all()
return ''
def get_all_posts(parser,token):
return AllPost()
get_all_posts = register.tag(get_all_posts)
Now, I try to load this template tag inside my template;
{% load get_all_posts %}
But this gives me with error, 'get_all_posts' is not a valid tag library: Template library get_all_posts not found, tried django.templatetags.get_all_posts,django.contrib.admin.templatetags.get_all_posts
What is the error in this template or have I missed something here.
With
loadyou need to use the name of the library, not the tag – sopostsin your case.(I assume you also have a blank
__init__.pyin the templatetags directory, and that the application is inINSTALLED_APPS).