我在url.py
中有这个from django.conf.urls.defaults import patterns, include, url
from django.views.generic import ListView
from blog.models import Post
urlpatterns = patterns('',
url(r'',
ListView.as_view(
queryset=Post.objects.all().order_by('-created')[:2],
template_name='blog.html'
)
),
)
,这在template / blog.html
中(% extends "base.html" %)
(% block content %)
(% for post in object_list %)
<h1>{{ post.title }}</h1>
<div class="post_meta">
on {{post.created}}
</div>
<div class="post_body">
{{post.body|safe|linebreaks}}
</div>
<div class="tags">
{% for tag in post.tags.all %}
<a href="blog/tag/{{tag}}">{{tag}}</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</div>
(% endfor %)
(% endblock %)
因此,正如教程所说,当我访问网站时,它应该包含2个博客,但是,当我去那里时,它会在浏览器中显示我:
(% extends "base.html" %) (% block content %) (% for post in object_list %)
on
(% endfor %) (% endblock %)
答案 0 :(得分:3)
是的问题是,在模板上,您必须使用{}
代替()
代替模板代码,{{}}
代表变量
{% extends "base.html" %}
{% block content %}
{% for post in object_list %}
<h1>{{ post.title }}</h1>
<div class="post_meta">
on {{post.created}}
</div>
<div class="post_body">
{{post.body|safe|linebreaks}}
</div>
<div class="tags">
{% for tag in post.tags.all %}
<a href="blog/tag/{{tag}}">{{tag}}</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</div>
{% endfor %}
{% endblock %}