我正在关注这本书" Django by Example",并且在第3章开头:扩展您的博客应用程序,我正在尝试加载我的blog_tags文件,但是我得到了这个AttributeError:
在template / home / ahmad / Documents / Coding / Django中通过示例/ Excercises / djangoblogapp / mysite / blog / templates / blog / post / list.html,第1行出错 '图书馆'对象没有属性'简单'
这是我的base.html:
{% load blog_tags %}
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %} {% endblock %}</title>
<link href="{% static "css/blog.css" %}" rel="stylesheet">
</head>
<body>
<div id = "content">
{% block content %}
{% endblock %}
</div>
<div id = "sidebar">
<h2> My Blog</h2>
<p> This is my blog. I've written {% total_posts %} posts so far. </p>
</div>
</body>
</html>
和我的帖子/ list.html:
{% extends "blog/base.html" %}
{% block title %}My Blog {% endblock %}
{% block content %}
<h1> My Blog </h1>
{% if tag %}
<h2>Posts tagged with "{{ tag.name }}"</h2>
{% endif %}
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="tags">
Tags:
{% for tag in post.tags.all %}
<a href="{% url "blog:post_list_by_tag" tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html" with page=posts %}
{% endblock %}
最后,这是我的templatetags / blog_tags.py:
from django import template
register = template.Library()
from ..models import Post
@register.simple._tag(name='useless')
def total_posts():
return Post.published.count()
我已经筋疲力尽了Google,我无法找到解决问题的方法。非常感谢你的帮助!
如果有帮助,我在Linux系统上使用Python 3.6.2和virtualenvwrapper。 Django版本是1.8.6
编辑:我得到的新错误:
在template / home / ahmad / Documents / Coding / Django By Example / Excercises / djangoblogapp / mysite / blog / templates / blog / base.html,第19行的错误 无效的块标记:&#39; total_posts&#39;
基本上,错误指向这行代码:
<p> This is my blog. I've written {% total_posts %} posts so far. </p>
我没有看到任何其他错误。
答案 0 :(得分:1)
如果您想使用simple-tags,请删除点
@register.simple_tag(name='useless')
# ^^^^^
def total_posts():
并在 base.html 中进行修改, 取代
written {% total_posts %} posts so far.
<!-- ^^^^^^^^^ -->
到简单标记的名称
written {% useless %} posts so far.
<!-- ^^^^^^^^^ -->