我在 Django中重新组合有一个问题。情况是:我有一个views.py的字典,我需要做以下事情:
Box A_name:
1 section:
1.1 element 1
...
1.n element n
N section:
N.1 element
...
N.m element
Box B_name:
...
html中的代码是:
{% load get_boxfilter %}
{% regroup all_boxes|dictsort:"box_type" by box_type as type %}
<ul>
{% for pos in type %}
<li> Box {{ pos.grouper|get_boxfilter }}
<ul>
{% for item in pos.list %}
<li> element {{ item.name }} section {{ item.section }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
get_boxfilter:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from django import template
register = template.Library()
@register.filter(name='get_groupfilter')
def get_boxfilter(value):
gb_list = [u"NULL", u"A_name", u"B_name", u"N_name" ]
return gb_list[int(value)]
它通过盒子名称重新组合:
Box A_name:
1. element 1 section 1
2. element 2 section 2
3. element 3 section 1
Box B_name:
...
但是如何按“部分”(int值)分组?
Box A_name:
1 section:
1.1 element 1
1.2 element 3
2 section:
2.1 element 2
谢谢