i created two model name categories and subcategories by which if i create a category name merch so under that i can create sub categories name t shirt, hoddies, shirt so i linked categories with foreign key in subcategories so i want to render a dropdown menu in which on top categories will show up and under that all the sub categories related to categories but i am unable to achieve that i tried this
my models.py
class Categories(models.Model):
name = models.CharField(max_length=100, blank=False)
joined_date = models.DateTimeField(default=timezone.now,editable=False)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Subcategories(models.Model):
categories = models.ForeignKey(Categories, on_delete=models.CASCADE)
name = models.CharField(max_length=200, blank=False)
joined_date = models.DateTimeField(default=timezone.now,editable=False)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
and my views.py
class home(View):
def get(self, request,):
category_list = Categories.objects.all()
return render (request, 'home.html', {'category_list': category_list })
and my html
<ul class="navbar-nav m-auto">
{% for category in category_list %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle category" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
{{ category.name }}
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item text-white" href="#"></a></li>
</ul>
</li>
{% endfor %}
my urls.py
path('',home.as_view(),name='home' ),
what it does it load the categories name but unable to subcategories name under the dropdown menu
here is the pic for you better understanding