2021-02-28

Placing items below category in template

So I wrote a small shopping list application using Django. Users may enter their desired items which are stored in a database alongside a category they reside in to make the list presented to the user more clean and giving them (the users) a good overview of what they are going to buy. The goal is to show the user a list which is categorized, something like this:

VEGETABLES

  • Paprika
  • Tomatoes

VARIOUS

  • Toothpaste
  • Toilet Paper

And so on. I have like five categories saved in my database and users may choose one corresponding category once they add an item to the list below which the item will be displayed in the list.

These are my database models:

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=20)
    tag = models.CharField(max_length=2)

    def __str__(self):
        return self.name

class Item(models.Model):
    text = models.CharField(max_length=40)
    count = models.CharField(max_length=100)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    complete = models.BooleanField(default=False)
    

    def __str__(self):
        return self.text

this is my views.py

def items(request):
    item_list = Item.objects.order_by('id')
    categories = Category.objects.all()
    form = ItemForm()
    context = {'item_list' : item_list, 'form' : form, 'categories' : categories}
    return render(request, 'buyit/index.html', context)

and this is my index.html (very basic, stripped off all the css and html stuff):

        

I got this functions from the Jinja2 template from a code snippet and it might be, that I didn't understand it correctly. However, the debugger tells me:

TemplateSyntaxError at /
Could not parse the remainder: '()' from 'category.item_set.all()'

Any hints on what I am doing wrong? Simply displaying the categories worked fine but after adding

    

things crashed. Any help is highly appreciated!

Thanks in advance!



from Recent Questions - Stack Overflow https://ift.tt/3bIyO3P
https://ift.tt/eA8V8J

No comments:

Post a Comment