Trying to make a linkdirectory page with categories and subcategories, have the following so far, problem is that i can't get the category as a link
尝试用类别和子类别创建一个linkdirectory页面,到目前为止,问题是我不能将类别作为链接。
models.py
class Category(models.Model):
"""Category"""
name = models.CharField(max_length=50)
slug = models.SlugField()
def save(self, *args, **kwargs):
#self.slug = slugify(self.name)
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
class SubCategory(models.Model):
"""Sub Category"""
category = models.ForeignKey(Category)
name = models.CharField(max_length=50)
slug = models.SlugField()
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(SubCategory, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
and
和
views.py
@require_GET
def links(request):
"""Linkdirectory Page"""
categories = Category.objects.all()
subcategories = SubCategory.objects.all()
return render(request, 'links.html', {'categories': categories, 'subcategories': subcategories,})
in order to group the subcategories by category i used the following in
为了将子类别按类别进行分组,我使用了以下方法
{% regroup subcategories|dictsort:"category_id" by category as categories_list %}
<ul>
{% for categories in categories_list %}
<li>{{ categories.grouper }}
<li> <a href="/links/{{ categories.slug }}">{{ categories.name }}</a></li>
<ul>
{% for item in categories.list %}
<li><a href="/links/{{ item.slug }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
This all works nicely grouping the subcats together
这些都可以很好地将子猫科动物分组在一起
CAT1 --subcatLINK1 --subcatLINK2
CAT1——subcatLINK1 subcatLINK2
but what i would like to have is
但是我想要的是
CATLINK1 --subcatLINK1 --subcatLINK2
CATLINK1——subcatLINK1 subcatLINK2
Not sure how and if its possible to replace the grouper string with a custom link as like the subcategory link or if i'm going the right way around this
不知道如何以及是否可以用自定义链接替换grouper字符串,就像子类别链接一样,或者我是否按照正确的方式
Edited, adding Bakkals approach below: EDIT AGAIN ;) nevermind, i had to update the naming scheme aswell obviously, below code is the working version. trying to use category.subcategory_set.all as suggested by Bakkal
编辑,添加Bakkals方法:再编辑;)nevermind,我必须更新命名方案,显然,下面的代码是工作版本。尝试使用category.subcategory_set。所有这些都是贝克尔建议的
{% for category in categories %}
<ul>
<li><a href="/links/{{ category.slug }}">{{ category.name }}</a>
{% for subcategory in category.subcategory_set.all %}
<ul>
<li><a href="/links/{{ subcategory.slug }}">{{ subcategory.name }}</a></li>
</ul>
{% endfor %}
</li>
</ul>
{% endfor %}
</li>
</ul>
{% endfor %}
1 个解决方案
#1
1
Since SubCategory
contains a ForeignKey to Category
, You can use category.subcategory_set
to get the subcategories.
因为子类别包含一个类别的外键,你可以使用Category。subcategory_set用来获取子类别。
I find this simpler than manually doing the grouping you did by the category_id
我发现这比手工按category_id进行分组要简单
So you can e.g. pass categories
as a queryset of all Category
objects as the context to your template, and then within the template
因此,您可以将类别作为所有类别对象的查询集作为上下文传递给模板,然后在模板中传递
{% for category in categories %}
...
{% for subcategory in category.subcategory_set.all %}
...
{% endfor %}
{% endfor %}
#1
1
Since SubCategory
contains a ForeignKey to Category
, You can use category.subcategory_set
to get the subcategories.
因为子类别包含一个类别的外键,你可以使用Category。subcategory_set用来获取子类别。
I find this simpler than manually doing the grouping you did by the category_id
我发现这比手工按category_id进行分组要简单
So you can e.g. pass categories
as a queryset of all Category
objects as the context to your template, and then within the template
因此,您可以将类别作为所有类别对象的查询集作为上下文传递给模板,然后在模板中传递
{% for category in categories %}
...
{% for subcategory in category.subcategory_set.all %}
...
{% endfor %}
{% endfor %}