My category are not showing on header menu, Please let me the process for this. If i use code on home.html file then all category are showing on homepage but when i click on any category page it's not showing.
Here are the models.py file...
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='websubcategory', 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 class SubChildCategory(models.Model): subcat=models.ForeignKey('WebSubCategory', related_name='subchild', on_delete=models.CASCADE, blank=True, null=True, verbose_name='Select Subcategory') 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="SubChildCategory" def __str__(self): return self.name And here are my views.py file
def header(request): category_list = WebCategory.objects.order_by('-created_at')[:5] context_dict={'webcat':category_list,} return render(request, 'layouts/header.html', context_dict) Here are y urls.py file..
from django.urls import path, re_path from . import views from django.conf.urls.static import static from django.conf import settings from django.conf.urls import url, include urlpatterns = [ re_path(r"^$", views.home, name="home"), re_path(r"blog/$", views.blog, name="blog"), url(r"^(?P<pk>\d+)/$", views.show_blog, name="show_blog"), path('<slug>/', views.show_page, name="show_page"), ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) An here are my header.html file...
<nav class="collapse"> <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 category.websubcategory.all %} <li> <div class="dropdown-mega-content"> <div class="row"> {% for subcategory in category.websubcategory.all|slice:":5" %} <div class="col-lg-3"> <span class="dropdown-mega-sub-title">{{subcategory.name}}</span> {% for subchildcat in subcategory.subchild.all %} <ul class="dropdown-mega-sub-nav"> <li><a class="dropdown-item" href="{% url 'show_page' subcategory.slug %}">{{subchildcat}}</a></li> </ul> {% endfor %} </div> {% endfor %} </div> </div> </li> {% else %} <p>No category Found</p> {% endif %} </ul> </li> {% endfor %} {% else %} <p>No Category Found</p> {% endif %} </ul> </nav>