0

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> 

1 Answer 1

1

You can't use a separate view to fetch a part of your page, unless you're making an ajax request from your main page. I mean, where is your header view ever called?

So you have two options:

  1. Add a context processor to your TEMPLATES setting that adds the categories to the context for every view. This way you can access the categories variable in your base template (the one you use to extend all your other templates). Check the docs to learn how to write your own context processor.

    def category_list(request): category_list = WebCategory.objects.order_by('-created_at')[:5] return {'webcat': category_list} 

    Adding this little context processor to your templates processing will add the webcat context variable to every rendered template.

  2. Fetch the list of categories dynamically from the page itself using javascript (AJAX). This means you add javascript to your base template to fetch a JSON list of your categories and update the menu. If you've never used javascript, you should learn a bit of jQuery and in particular search for "how to make ajax request with django".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.