[Answered ]-Django cannot access all objects in a model

2👍

Your code is:

context['category'] = Category.objects.all()

So it should be:

context['categories'] = Category.objects.all()

And in your template:

{% for category in categories %}
  {{ category.name }}
{% endfor %}

The output you got in your test makes sense:

[<Category: Something not so interesting>]

it’s an array with only one entry, this entry is an object of the class Category, and the string representation of it is “Something not …”

0👍

You need to iterate over the category, since it’s queryset. E.g. in your template, you can do

<ul>
{% for c in category %}
  <li> {{ c }} </li>
{% endfor %}
</ul>

0👍

category in template is queryset ( list of objects) not one single object. You need to iterate over it as

{%for c in category %}
    {{c.id}} : {{ c.other_attribute }}
{%endfor%}
👤Rohan

Leave a comment