Django迭代模板中的所有flatpages

时间:2010-09-22 08:23:42

标签: django

如何获取模板中所有flatpage对象的列表?

我没有使用开发版..

由于

2 个答案:

答案 0 :(得分:1)

开发版使用custom template tag来获取模板中的所有flatpage对象。如果您想立即使用此功能,则可以复制source code并将其添加为自定义标记。

警告:没有测试过这个。

答案 1 :(得分:1)

如果您有权访问呈现所需模板的视图,则可以从数据库中提取所有flatpages。这是一个非常原始且远非理想的解决方案:

在视图中:

from django.contrib.flatpages.models import Flatpage

...do your other view stuff

flatpages = Flatpage.objects.all() 
# You REALLY SHOULD filter() based on other properties of the Flatpages, 
# such as whether or not it requires login to view, or, importantly, 
# which Site it is available on (because not all Flatpages will 
# necessarily be available on the current Site)

...then pass the flatpages queryset into your view

在你的模板中:

<ul>
{% for flatpage in flatpages %}
     <li><a href="{{flatpage.url}}">{{flatpage.title}}</a></li>      
     {#Note that the page title may not be good link text #} 
{% endfor %}
</ul>