2

What am i getting this error for ?

models.py

class Category(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    def __str__(self):
        return self.name


class SubCategory(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    image_url = models.CharField(default=0, max_length=2000)
    price = models.IntegerField(default=0)

views.py

def category(request, pk):
    categories = Category.objects.get(id=pk)
    subcategories = SubCategory.objects.filter(category=categories)
    return render(request, 'category.html', {'categories': categories, 'subcategories': subcategories})

urls.py

urlpatterns = [
    path('', views.index),
    url(r'^category/(?P<pk>\d+)$', views.category, name='category'),
]

base.html

{% for category in categories %}
<a class="dropdown-item" href="{% url 'category' pk=category.id %}">{{ category.name }}</a>
{% endfor %}
1
  • unrelated but you can use category.subcategory_set.all() instead of manually filtering. Commented Mar 6, 2019 at 15:08

3 Answers 3

2

get returns a model instance, not a queryset (despite your misleading variable name):

categories = Category.objects.get(id=pk)  # instance, not queryset!

Hence:

{% for category in categories %}  # instance cannot be looped over!

produces the error that you encoutered.

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

1 Comment

Just {{ categories.name }} without a loop, for starters. But you should really change the name of that context variable to a singular one.
0

You are trying to get only one Category object in below file.

views.py

def category(request, pk):
        categories = Category.objects.get(id=pk) # Here you trying to get category
        subcategories = SubCategory.objects.filter(category=categories)
        return render(request, 'category.html', {
            'categories': categories, # categories is single object not iterable
            'subcategories': subcategories})

For solution you can either set categories = Category.objects.filter(id=pk) to your views.py or update your html template.

Comments

0

because, my error was related with queryset. what does it mean ? an array. and each array has its indexes, so, in this example, our 'categories' is an array and we must assign its first ([0]) index to a category:

def category(request, pk):
        categories = Category.objects.get(id=pk)
        subcategories = SubCategory.objects.filter(category=categories[0])
        return render(request, 'category.html', {'categories': categories, 'subcategories': subcategories})

1 Comment

Please consider adding a description of your code so we know what it does and why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.