0

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

1 Answers1

0

get_object_or_404() method returns single instance or None if it is not found. You can't iterate that way on object.

Try next

View

def category(request, slug):
    """Defines category views."""
    return render(request, 'categories_list.html', {'category': get_object_or_404(Category, slug=slug)})

Template

{{ category.slug }}
Tigran
  • 632
  • 1
  • 5
  • 21
  • Thanks mate i followed your advice but unfortunately it did not work....your code kind of resembles mine except mine is slightly long. The problem is on the template i think. Even when i use `categories= Category.objects.get(slug= slug) or categories= Category.objects.all(slug=slug` nothing is working. – Brendon-ZA May 06 '20 at 16:57
  • Difference is that you were doing if category in categories, it will not work. Can you show your model? Does it have property slug? Maybe you call it another way. – Tigran May 06 '20 at 17:06
  • That is why i used the if statement....the if statement did not return any errors like the for loop. i just want my categories to be displayed on my sidebar as url links that can be clicked and change the address from 127.0.000 to 127.0.000/electronics for example – Brendon-ZA May 06 '20 at 17:45
  • Here is my category model,.. `class Category(models.Model): name= models.CharField(max_length=100) slug= models.SlugField(max_length= 100, unique= True) class Meta: ordering = ('name',) verbose_name= 'category' verbose_name_plural= 'categories' def __str__(self): return self.name def get_absolute_url(self): return reverse('category', kwargs={'category_slug': self.slug}) `...i just added the get_absolute_url and i called it the template still nothing. The for loop didnt work it kept saying cannot iterate over object – Brendon-ZA May 06 '20 at 17:46
  • what {{ category }} displays? – Tigran May 06 '20 at 19:14