我創建我的應用程序內,一個名爲posts.py
文件下面的代碼裏面,我已經寫了templatetags
文件夾;的Django無法加載模板標籤
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)
現在,我嘗試在模板中加載此模板標籤;
{% load get_all_posts %}
但是這給了我的錯誤,'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
什麼是這個模板中的錯誤或有我錯過了一些東西在這裏。
嗯,我編輯了上面的代碼,但它仍然給我同樣的錯誤。 – Sandeep 2011-12-22 17:41:45
我發現我的錯誤,我只是加載與錯誤的名稱的文件。現在,我已成功加載標籤作爲{%負載帖子%} ADN然後加載功能{%get_all_posts%}。現在,我可以使用所有的上下文變量。它是如何完成的? – Sandeep 2011-12-22 19:05:01