Python

How to use objects.filters in Django?

  • django
  • python
  • filter

4 comments

Hi guys!

Thanks for the qn Baldy, and Manfred for the solution.

Here is a slightly longer answer to the example in the qn.



Firstly, we will need to satisfy this portion of the question -

"Create respective categories pages "/menu/{category_name}" to display all menu items from the database according to their categories.."

In order to do this, we need to make sure the /{category_name} urls are created, as follows:

in app > urls.py,

from django.urls import path
from . import views

urlpatterns = [
path(' ', views.index),
path('starters', views.starters), # for url path ~ .menu/starters
path('desserts', views.desserts), # for url path ~ .menu/desserts
path('mains', views.mains), # for url path ~ .menu/mains
path('beverages', views.beverages) # for url path ~ .menu/beverages
]

Next, to show the filtered data in their url paths respectively, in apps > views.py, add the following:

from django.http import HttpResponse
from django.shortcuts import render
from .models import Item

# View of the main page with all menu items
def index(request):
item = Item.objects.all()
return render(request, 'index.html', {'Menu': item})

# View of page filtered by category 'Starters'
def starters(request):
item = Item.objects.all().filter(category='Starters')
return render(request, 'index.html', {'Menu': item})

# View of page filtered by category 'Mains'
def mains(request):
item = Item.objects.all().filter(category='Mains')
return render(request, 'index.html', {'Menu': item})

# View of page filtered by category 'Desserts'
def desserts(request):
item = Item.objects.all().filter(category='Desserts')
return render(request, 'index.html', {'Menu': item})

# View of page filtered by category 'Beverages'
def beverages(request):
item = Item.objects.all().filter(category='Beverages')
return render(request, 'index.html', {'Menu': item})

This would work as required.

For further understanding, in apps > models.py, the code is as follows:

from django.db import models

# Create your models here.
class Item(models.Model):
product_name = models.CharField(max_length=45)
price = models.FloatField()
category = models.CharField(max_length=10)
image_url =models.CharField(max_length=255)

This is what 'Item.objects.all().filter(category='Desserts')' pulls data for the categories from.

Where do you indicate the specific categories? Ans: Not in the code, but via the client Django Admin Panel. Ensure that you get your cases correctly, as the above code runs case-sensitively.

That's all!

Hi Baldy,
I just realized I forget the double quote... so it should be:

def Starters(request):
starter = Menu.objects.filters(category="Starters")
return render(request, 'index.html', {'menu': starter})

I am not 100% sure what is the issue as I am unable to see your code.
But here is a good website to read about Python filter: https://pythonguides.com/python-django-filter/

Hello MAnfred after i use your way of doing it it shows,

AttributeError at /Menu/Starters/
'Manager' object has no attribute 'filters'

Hello,
If your column name is category, the code should be something like this..

def Starters(request):
starter = Menu.objects.filters(category=Starters)
return render(request, 'index.html', {'menu': starter})

The "Menu" in the render request, should be in small letters so as not to be confused or conflicted with the Menu table you're pulling from.
The "menu" in {'menu': starter} is used for the HTML side printing.

Hope this helps!