我喜欢下载列表,其中包含所有带有类别的文件=='下载'在所有可用类别中。 (ManyToMany Field)
>>>from feincms.module.medialibrary.models import MediaFile, Category
>>>MediaFile.objects.filter(categories=1) #my download pk, these files I need
>>>Category.objects.all() # these categories I need
models.py: https://github.com/feincms/feincms/blob/master/feincms/module/medialibrary/models.py
它必须是来自:
的混合物{% for category in category %}
<h1>{{ category }}</h1>
{% for file in category.mediafile_set.all %}
<li> {{ file }} </li>
{% endfor %}
{% endfor %}
但不是所有文件,只有带有类别的文件(或者说“标记”)&#39;下载&#39;。
和:
{% regroup file by categories.all as media_list %}
<ul>
{% for categories in media_list %}
<li>{{ categories.grouper }}
<ul>
{% for file in categories.list %}
<li>{{ file }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
这些文件很好,但类别位置和格式都不正确。
我需要这样一棵树:
类别1 #下载类别,所有文件
第2类
第3类
my views.py this:
from feincms.module.medialibrary.models import MediaFile, Category
def medialistview(request):
file = MediaFile.objects.filter(categories=1)
category = Category.objects.all()
return render_to_response('media.html',{
'file': file, 'category': category,
},context_instance=RequestContext(request))
我是初学者,也许这很容易......
答案 0 :(得分:0)
您可以使用自引用template includes,然后在迭代时将categories
变量作为每个类别的当前子项传递。
让我们说:category_tree.html
{% regroup file by categories.all as media_list %}
<ul>
{% for category in media_list %}
<li>{{ category.grouper }}
{% include "category_tree.html" with categories=category.children %}
<ul>
{% for file in category.list %}
<li>{{ file }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>