Hi guys i am new to django...i been watching youtube videos and reading books on Django but am still struggling with templates. I am working on a ecommerce project and i would love a bit of help with templates. So i want my template to display a list of categories as links on a sidebar. I have defined a slug field in my category models and i have managed to map a url...but i am still not getting a list of categories on my index page sidebar.
This is my url pattern and this is working perfect. When i click 127.0.0.1.000/food it's working (food is a category)
path('<slug:category_slug>/', views.category, name='category'),
the view function
def category(request, category_slug):
"""Defines category views"""
categories= get_object_or_404(Category, slug= category_slug)
context = {'categories': categories}
return render(request, "categories_list.html", context)
This is the categories_list.html template that i need help with
<h3> Shop by Category </h3>
{% if category in categories %}
<li>
<a href ="{{category.slug}}"> {{category.name}}</a>
</li>
{% endif %}
My wish is to have the categories displayed on the sidebar of my index page as links. I have used {% include 'category_list.html' %}
on my index page template, and its only displaying the Shop by Category heading instead of the categories when i am on the index page. I have tried the for loop in my template but if didn't work, it kept on saying category object not iterable...so i ended up using the if statement. Any help will be appreciated