我正在为我的电子商务网站构建类别和子类别树。类别模型本身具有TreeForeignKey,因此类别可以具有许多其他子类别作为子类别。在product.html路线中,我成功地使用 {&recursetree [models]&} 标签渲染了类别树(请参见下面的第一张图片)。当用户单击说“泵”链接时,该用户将被带到另一个仅显示“泵”及其子类别的页面。但是,当我尝试使用 {&recursetree [models]&} 复制相同的内容时,它没有效果。我只看到“泵”的父级,而没有子级子类别。我已经确保将treequeryset传递到recursetree标记中,以免遇到“对象不可下标错误”。但是,我遇到了一个障碍,不知道我的代码出了什么问题。
models.py
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey, TreeManyToManyField
from ckeditor_uploader.fields import RichTextUploadingField
from django.urls import reverse
# Create your models here.
class Category(MPTTModel):
Status = (
('True', 'True'),
('False', 'False'),
)
bool_choices = (
(True, 'Yes'),
(False, 'No'),
)
parent = TreeForeignKey('self', blank = True, null = True, related_name = 'children', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
slug = models.SlugField(unique=True, null=False)
description = models.TextField()
status = models.CharField(max_length=10, choices = Status)
is_active = models.BooleanField(choices=bool_choices)
meta_keywords = models.CharField(max_length=255)
meta_description = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
image = models.ImageField(blank = True, upload_to = 'images/')
class Meta:
db_table = 'categories'
verbose_name_plural = 'Categories'
def __str__(self):
return f"{self.name}"
class MPTTMeta:
order_insertion_by = ['name']
def get_absolute_url(self):
return reverse('category_detail', kwargs={'slug':self.slug})
views.py
def product(request):
category = Category.objects.all()
context = {'category': category}
return render(request, 'product.html', context)
def category(request, slug):
category = Category.objects.all().filter(slug=slug)
context = {'category': category}
return render(request, 'category.html', context)
urls.py
from django.urls import path
from . import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path("product/", views.product, name='product'),
path("<str:slug>/", views.category, name='category'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
product.html
{% extends 'layout.html' %}
{% block title %}
{% endblock %}
{% block body %}
<div>
{% load mptt_tags %}
<div class = "container">
<ul class="root">
{% recursetree category %}
<li>
<a href="{% url 'category' node.slug %}">{{ node.name }}</a>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
</div>
</div>
{% endblock %}
[在product.html中成功呈现的类别及其子类别] [1]:https://i.stack.imgur.com/WO2ZH.png
category.html
{% extends 'layout.html' %}
{% block title %}
{{ category.id }}
{% endblock %}
{% block body %}
{% load mptt_tags %}
<div class = "container">
<ul class="root">
{% recursetree category %}
<li>
<a href="{% url 'category' node.slug %}">{{ node.name }}</a>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
</div>
{% endblock %}
[泵的子类别未能在category.html中呈现] [2]:https://i.stack.imgur.com/6DRkQ.png
答案 0 :(得分:0)
category = Category.objects.all()。filter(slug = slug)的输出仅是
如果我还需要渲染子代,则需要调用以下行以获取子代和模型实例本身。
category = category.get_descendants(include_self=True)