blu*_*ues 1 django django-templates django-views
假设我有一个国家/地区列表,我想按大陆分组显示它们。我的 models.py 看起来像这样
class Countries(models.Model):
name = models.CharField(max_length=200)
continent = models.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud)
而且,我的views.py 看起来像这样:
class ListCountriesView(ListView):
model = Countries
template_name = 'country_list.html'
Run Code Online (Sandbox Code Playgroud)
我的 html 显示以下列表
Asia
Japan
Asia
S.Korean
Asia
China
Asia
India
Europe
Germany
Europe
France
Europe
Belgium
Africa
Ghana
Run Code Online (Sandbox Code Playgroud)
我的country_list.html 看起来像这样:
<html>
<table>
<tr><th>Country</th></tr>
{% for country in object_list%}
<td>kpi.continent</td>
<tr><td>country.name</td></tr>
</table>
</html>
Run Code Online (Sandbox Code Playgroud)
我想显示一次大陆的名称,然后显示相应的国家。我不知道如何按大陆对列表进行分组。有没有可以在 django 模板中使用的 flllter 函数?
是的。肯定有:
{% regroup object_list by continent as continents_list %}
<ul>
{% for continent in continents_list %}
<li>{{ continent.grouper }}
<ul>
{% for item in continent.list %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2785 次 |
| 最近记录: |