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

时间:2019-07-24 06:39:14

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

我在Django的WebSubCategory下显示WebCategory时遇到了一些问题。我已经显示了WebCategory,但无法显示WebSubCategory下的WebCategory

这是我的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':category_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.name}}
                                                    </a>
                                                    {% if websubcat.webcat %}
                                                    <ul class="dropdown-menu">
                                                        <li>
                                                            <div class="dropdown-mega-content">
                                                                <div class="row">
                                                                    {% for subcategory in websubcat.webcat %}
                                                                    <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="elements-accordions.html">{{subcategory.name}}</a></li>
                                                                        </ul>
                                                                    </div>
                                                                    {% endfor %}
                                                                </div>
                                                            </div>
                                                        </li>
                                                    </ul>
                                                    {% else %}
                                                    <p>Not Found Submenu</p>
                                                    {% endif %}
                                                </li>
                        {% endfor %}
                        {% else %}
                        <p>No Category Found</p>
                        {% endif %}

博客                     

请帮助我在WebSubCategory下显示WebCategory

1 个答案:

答案 0 :(得分:0)

{% if webcat_set.all|length > 0 %}
    {% for subcategory in webcat_set.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="elements-accordions.html">      {{subcategory.name}}</a></li>                                                                          
            </ul>
        </div>
    {% endfor %}
{% endif %}