title: Django. Categories and subcategories
body: <p>I want to make a navigation through categories and subcategories in Django.</p>
  
  <p>Now I have this:</p>
  
  <p>127.0.0.1:8000/products/last subcategory/product slug</p>
  
  <p>and I want to make</p>
  
  <p>127.0.0.1:8000/products/category/subcategory/subsubcategory/.../product slug</p>
  
  <p>smth like  this:</p>
  
  <ul>
  <li>Food
  
  <ul>
  <li>Vegetables
  
  <ul>
  <li>Carrot</li>
  <li>Broccoli</li>
  <li>Tomatoes</li>
  </ul></li>
  <li>Fruits
  
  <ul>
  <li>Apple</li>
  <li>Pear</li>
  </ul></li>
  </ul></li>
  <li>Beverages</li>
  </ul>
  
  <p>my models.py:</p>
  
  <pre><code>class Product(models.Model):
      title       = models.CharField(max_length=120)
      slug        = models.SlugField(unique=True)
      category    = models.ForeignKey('Category', on_delete=models.CASCADE) 
      description = models.TextField(blank=True,null=True)
  
      def save(self, *args, **kwargs):
          self.slug = slugify(self.title)
          super(Product,self).save(*args, **kwargs)
  
      def get_absolute_url(self):
          return self.slug
  
  class Category(models.Model):
      name = models.CharField(max_length=200)
      slug = models.SlugField(unique=True)
      parent = models.ForeignKey('self',blank=True, null=True         ,related_name='child', on_delete=models.CASCADE)
      class Meta:
          unique_together = ('slug', 'parent',)    
          verbose_name_plural = "categories"   
  
      def __str__(self):                           
          full_path = [self.name]            
          k = self.parent
          while k is not None:
              full_path.append(k.name)
              k = k.parent
  
          return ' -&gt; '.join(full_path[::-1])
  </code></pre>
  
  <p>my urls.py</p>
  
  <pre><code>path('', ProductList, name='product-list1'),
  path('&lt;category_slug&gt;/', products_by_category, name='product-categories'),
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def ProductList(request):
      products = Product.objects.all()
      products = products.order_by('-publish')
      categories = Category.objects.filter(parent__isnull=True)
      context = {
          'products':products,
          'categories':categories,
      }
      template = 'products/product_list.html'
      return render(request, template ,context)
  
  
  def products_by_category(request,category_slug):
      products = Product.objects.all()
      categories = Category.objects.filter(parent__isnull=True)
      slug = category_slug
      if slug:
          category_s = get_object_or_404(Category,slug = slug)    
          products = products.filter(category = category_s)
      context = {
          'products':products,
          'categories':categories,
          'category':category_s,
          'page_obj':page_obj,
      }
      template = 'products/products_by_category_list.html'
      return render(request,template,context)
  </code></pre>
  
  <p>And how can I display all products that are in one category.
  By the way, is it possible to show all items that belong to one parent category if sorting by that parent cat.?
  For example all food </p>
  
  <ul>
  <li>Food
  
  <ul>
  <li>Vegetables
  
  <ul>
  <li>Carrot</li>
  <li>Broccoli</li>
  <li>Tomatoes</li>
  </ul></li>
  <li>Fruits
  
  <ul>
  <li>Apple</li>
  <li>Pear</li>
  </ul></li>
  </ul></li>
  <li>Beverages</li>
  </ul>