我在 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'
)
),
)
这在模板/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 %)