Django无法加载模板标签 [英] Django could not load template tag

查看:134
本文介绍了Django无法加载模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中创建了一个 templatetags 文件夹,并在名为 posts.py 的文件中创建了一个以下代码;

  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(解析器,令牌):
返回AllPost()
get_all_posts = register.tag(get_all_posts)

现在,我尝试在我的模板中加载这个模板标签;

  {%load get_all_posts%} 

但这给我错误,'get_all_posts '不是有效的标签库:没有找到模板库get_all_posts,尝试django.templatetags.get_all_posts,django.contrib.admin.templatetags.get_all_posts



这个t的错误是什么?

解决方案

使用加载使用图书馆的名称,而不是标签 - 所以帖子在你的情况下。



(我假设你在templateatetags目录中还有一个空白的 __ init __。py ,应用程序在 INSTALLED_APPS 中)


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 load you need to use the name of the library, not the tag - so posts in your case.

(I assume you also have a blank __init__.py in the templatetags directory, and that the application is in INSTALLED_APPS).

这篇关于Django无法加载模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆