So im trying to build this clothing website and im now in the part which an html page displays all the shoes, for this i have a field called category, however, my problem is that whenever I use (category__contains=“Shoes”) it shows the “Related Field got invalid lookup: contains” error.
To clarify the views.py function thats in charge of displaying the shoes is the one called shoes.
Here is my views.py code:
from django.shortcuts import render
from .models import ClothingItem,Categorie,SubCategorie
# Create your views here.
def home(request):
return render(request,'item_type/home.html',{})
def show_category(request, category_id):
category = Categorie.objects.get(id=category_id)
sub_category = SubCategorie.objects.all()
context = {'category':category,'sub_category':sub_category}
return render(request, 'item_type/show_category.html',context)
def clothing(request):
sub_category = SubCategorie.objects.all()
context = {'sub_category':sub_category}
return render(request,'item_type/clothing.html',context )
def show_subcategory(request, subcategory_id):
sub_category = SubCategorie.objects.get(id=subcategory_id)
item_list = ClothingItem.objects.filter(sub_category=sub_category)
context = {'sub_category':sub_category,'item_list':item_list}
return render(request, 'item_type/show_subcategory.html',context)
def all_items(request):
items_list = ClothingItem.objects.all()
return render(request, 'item_type/clothing_list.html',{'items_list':items_list})
def shoes(request):
shoe_list = ClothingItem.objects.filter(category__contains='Shoes')
return render(request, 'item_type/shoes.html',{'shoe_list':shoe_list})
def category(request):
sub_category = SubCategorie.objects.all()
category_list = Categorie.objects.all()
return render(request, 'item_type/categories.html',{'category_list':category_list,'sub_category':sub_category})
def search_item(request):
if request.method == "POST":
searched = request.POST['searched']
items = ClothingItem.objects.filter(name__contains=searched)
return render(request, 'item_type/search_item.html',{'searched':searched,'items':items})
else:
return render(request, 'item_type/search_item.html',{})
Here is my models.py code (please ignore the spelling of category and sub-category):
from django.db import models
from django.urls import reverse
import uuid
# Create your models here.
class ClothingItem(models.Model):
category = models.ForeignKey('Categorie',null=True,on_delete=models.CASCADE)
sub_category = models.ForeignKey('SubCategorie',null=True,blank=True,on_delete=models.CASCADE)
name = models.CharField( max_length=50)
brand = models.ForeignKey('Brand',null=True,on_delete=models.CASCADE)
description = models.TextField(max_length=1000,blank=True)
item_image = models.ImageField(null=True,blank=True, upload_to="images/")
sale = models.BooleanField('Sale', null=False,blank=False)
def __str__(self):
return self.name
class Categorie(models.Model):
category = models.CharField(max_length=100)
def __str__(self):
return self.category
class SubCategorie(models.Model):
sub_category = models.CharField('Clothing Type',max_length=100)
def __str__(self):
return self.sub_category
class Sale(models.Model):
onSale = models.BooleanField(null=False, blank=False)
def __str__(self):
return self.onSale
class Brand(models.Model):
brand_name = models.CharField(max_length=100)
class Meta:
ordering = ['brand_name']
def get_absolute_url(self):
return reverse("brand_detail", kwargs={"pk": self.pk})
def __str__(self):
return self.brand_name
Here is the html page which should bring up the shoes:
{% extends 'item_type/base.html' %}
{% block content %}
<h1>List of shoes</h1>
<br/>
{% for i in shoe_list %}
<div class="card">
<div class="card-header">
{{i}}
</div>
<div class="card-body">
<p class="card-text"> <strong> Brand: {{i.brand}} </strong>
{% if i.item_image %}
<br/>
<img src="{{i.item_image.url}}">
{% endif %}
<br/>{{i.description}}</p>
</div>
</div>
<br/>
<br/>
{% endfor %}
{% endblock %}