如何在Django中特定类别下显示子类别?

时间:2019-07-24 08:18:15

标签: django django-models django-rest-framework django-templates django-views

我的代码存在一些问题,同一子类别显示在所有类别下,如果一个类别没有子类别,那么该类别也显示了子类别,我想显示一个类别是否具有子类别,那么它应该是在下拉列表中显示,否则应为空白。

这是我的models.py文件...

class WebCategory(models.Model):
  name = models.CharField(max_length=50, unique=True, verbose_name='Category name')
  slug = models.SlugField(verbose_name='Slug')
  title = models.CharField(max_length=165, null=True)
  metadesc = models.TextField(max_length=165, null=True)
  created_at = models.DateTimeField(auto_now_add=True, null=True)
  updated_at = models.DateTimeField(auto_now=True)

  class Meta:
      verbose_name_plural = 'WebCategory'

  def save(self, *args, **kwargs):
    self.slug = slugify(self.name)
    super(WebCategory, self).save(*args, **kwargs)

 def __str__(self):
    return self.name

class WebSubCategory(models.Model):
  category = models.ForeignKey('WebCategory', related_name='subcategory', on_delete=models.CASCADE, blank=True,
                             null=True, verbose_name='Select category')
  name = models.CharField(max_length=50)
  slug = models.SlugField(unique=True, null=True)
  title = models.CharField(max_length=100, null=True)
  metadesc = models.TextField(max_length=165, null=True)
  description = RichTextField(blank=True, null=True)
  created_at = models.DateTimeField(auto_now_add=True, null=True)
  updated_at = models.DateTimeField(auto_now=True)

  class Meta:
      verbose_name_plural = 'WebSubCategory'

  def __str__(self):
      return self.name

这是我的views.py文件

def home(request):                                                                  
  context = RequestContext(request)                                               
  category_list = WebCategory.objects.order_by('-created_at')[:5]                 
  subcategory_list = WebSubCategory.objects.order_by('-created_at')[:5]           
  context_dict = {'webcat': category_list, 'websubcat':subcategory_list}          
  return render_to_response('home.html', context_dict, context)                   

这是我的header.html文件。

 <ul class="nav nav-pills" id="mainNav">
     {%if webcat %}
     {% for category in webcat %}
        <li class="dropdown dropdown-mega">
            <a class="dropdown-item dropdown-toggle" href="JavaScript:void()">
                {{category}}
            </a>
            <ul class="dropdown-menu">
                {% if websubcat %}
                <li>
                    <div class="dropdown-mega-content">
                        <div class="row">
                            {% for subcategory in websubcat.all %}
                            <div class="col-lg-3">
                                <span class="dropdown-mega-sub-title">Elements 1</span>
                                <ul class="dropdown-mega-sub-nav">
                                    <li><a class="dropdown-item" href="{{subcategory.slug}}">{{subcategory.name}}</a></li>
                                </ul>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </li>
                {% else %}
                <p>No category Found</p>
                {% endif %}
            </ul>
        </li>
    {% endfor %}
 {% else %}
<p>No Category Found</p>
{% endif %}

请指导我如何显示类别下的特定子类别(如果类别具有子类别),现在我将在所有类别下获得子类别。

1 个答案:

答案 0 :(得分:0)

它向您显示相同的内容,因为这就是您要执行的操作;您总是在同一集合上进行迭代。

您应该遵循模板中的关系,而不是从视图中发送WebSubCategory。您尚未显示模型,所以我不知道确切的字段名称,但是例如:

{% for subcategory in category.subcategory.all|slice:":5" %}
<div class="col-lg-3">
    <span class="dropdown-mega-sub-title">Elements 1</span>
    <ul class="dropdown-mega-sub-nav">
        <li><a class="dropdown-item" href="{{subcategory.slug}}">{{subcategory.name}}</a></li>
    </ul>
</div>
{% endfor %}

还请注意,您不应在视图中使用RequestContext和render_to_response;请使用render快捷方式。

def home(request):                                                                  
  category_list = WebCategory.objects.order_by('-created_at')[:5]                 
  context_dict = {'webcat': category_list}
  return render(request ,'home.html', context_dict)