I cannot create category and sub-categories list in home page,django

209 views Asked by At

I can create a category page with sub-categories in a new template but i cannot list all my sub-categories in home page. All i need is a logic to work in home page.

Here is my models.py

class Category(MPTTModel):
    name = models.CharField(max_length=100,unique=True)
    parent = TreeForeignKey('self',null=True,blank=True,related_name='children',db_index=True,on_delete = models.CASCADE)
    slug = models.SlugField()

    class MPTTMeta:
        order_insertion_by = ['name']
    
    def get_slug_list(self):
        try:
            ancestors = self.get_ancestors(include_self=True)
        except:
            ancestors = []
        else:
            ancestors = [ i.slug for i in ancestors]
        slugs = []
        for i in range(len(ancestors)):
            slugs.append('/'.join(ancestors[:i+1]))
        return slugs

    def __str__(self):
        return self.name
    
    def get_absolute_url(self):
        return reverse('product:product-category',args=[self.slug])

class Product(models.Model):
    category = TreeForeignKey(Category,null=True,blank=True,on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200,unique=True)
    def get_absolute_url(self):
        return reverse('product:product-detail',kwargs={
            'slug':self.slug,
            
        })

here is my views.py

def product_list(request,category_slug=None):
    category = None
    categories = Category.objects.all()
    products = Product.objects.filter(available=True)
    if category_slug:
        category = get_object_or_404(Category,slug=category_slug)
        products = products.filter(category=category)
    
    return render(
        request,'product/home.html',{
            'category':category,
            'categories':categories,
            'products':products
        }
    )


def show_category(request,hierarchy= None):
    category_slug = hierarchy.split('/')
    parent = None
    root = Category.objects.all()

    for slug in category_slug[:-1]:
        parent = root.get(parent=parent, slug = slug)

    try:
        instance = Category.objects.get(parent=parent,slug=category_slug[-1])
    except:
        instance = get_object_or_404(Product, slug = category_slug[-1])
        return render(request, 'product/product.html', {'instance':instance})
    else:
        return render(request, 'product/categories.html', {'instance':instance})
 

I used this logic in my template but it doesn't work. So any help to display the categories and sub-categories list would be massively appreciated.

      {% for c in categories %}
        <a href="{{ c.get_absolute_url }}">{{ c.parent }}</a>
       {% for i in c.children.all %}
       <a href="#">{{ i.name }}</a>
       {% endfor %}
        
        
      {% endfor %}
1

There are 1 answers

1
addu390 On

The issue you are facing is not very clear. But irrespective, have a look at this template for the categories view,