【发布时间】:2011-05-08 08:27:09
【问题描述】:
我正在为 Django 编写一个类似博客的简单应用程序,并试图获得这样的效果:首页的帖子限制为 5 个,并具有一次列出大约 100 个帖子的综合存档。 (100 是不现实的,只是把数字扔在那里)
由于两个页面之间的博客文章块减去显示的数字后看起来完全一样,我想将相应的 HTML 放在一个单独的模板中,我可以包含或链接到正在呈现的实际模板。我查看了文档,include 标记看起来很有希望,但它显然呈现在当前上下文之外,这对我的事业没有帮助,因为它不会让对象循环通过。除此之外,我看不到任何其他方式可以做我想做的事。这是可能的还是我只是运气不好而不得不违反 DRY?下面的代码可以让您了解我想要什么。
谢谢
#######################
# news/frontpage.html #
#######################
{% extends "news/base.html" %}
{% block site_title %} - Front Page{% endblock %}
{% block center_col %}
{{ block.super }}
<a href="/news/">View Older Blog Posts</a>
{% endblock %}
{% block blog_rows %}
{% for object in object_list %}
# Blog post content would go here, however it is to be included.
{% endfor %}
{% endblock %}
【问题讨论】:
标签: python django django-templates