(碰巧,我删除了我的最后一个问题)
如何在我的视图功能中创建面包屑?
class Category(MPTTModel):
name = models.CharField(max_length=50, verbose_name=u'Name')
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
slug = models.SlugField()
class Product(models.Model):
name = models.CharField(max_length=50, verbose_name=u'Name')
slug = models.SlugField()
#category = models.ManyToManyField(Category, verbose_name=u'Category')
category = models.ForeignKey(Category)
#views
def post_content(request, product_id):
product = get_object_or_404(Product, id = product_id)
return render_to_response('product_info.html', {'product':product},context_instance=RequestContext(request))
product_info.html
{{ product.name }}
我想要在我的单个帖子信息 ( post_content
) 面包屑中包含类别。像这样的东西:类别>子类别>子子类别.,,,
有人告诉我使用 get_ancestors:
例子:
{% for parent in category.get_ancestors %}
<a href="{{ parent }}">{{ parent.name }}</a> >
{% endfor %}
{{ category.name }}
如何用我的模型实现这一点?
谢谢你。我将感激不尽。