\n
views.py
\ndef searchall(request):\n getobject = request.GET['search']\n getcategory = request.GET['category']\n if getcategory == 'All':\n getcategory = ""\n search = Book.objects.filter(name__icontains=getobject, category__icontains=getcategory)\n oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)\n lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)\n hehe = CartItem.objects.filter(user=request.user)\n category = Category.objects.all()\n\n fianlprice = 0\n for item in hehe:\n fianlprice += item.book.price\n books = Book.objects.all()\n return render(request, 'main/search.html', {'books':search, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})\n\n
\nsearch.html
\n<h1>search</h1>\n<h1>{{ error }}</h1>\n<h1>Your cart currently costs ${{ price }}</h1>\n<form method="GET" action="">\n <input type="text" placeholder="Search here" name="search" id="search">\n <button type="submit">Search</button>\n</form>\n{% for book in books %}\n<h3>{{ book.name }}</h3>\n<img src= "/media/{{ book.image }}" alt="">\n<p>{{ book.description }}</p>\n{% if book.id in cart %}\n <form method="POST" action="/removefromcartforproducts/">\n {% csrf_token %}\n <button type="submit" name="removeid" value="{{ book.id }}">remove item from cart</button>\n </form>\n {% elif book.id in order %}\n <h3>You already own this</h3>\n {% else %}\n <form method="POST" action="/addtocartforproducts/">\n {% csrf_token %}\n <button type="submit" name="bookid" value="{{ book.id }}">Add to cart</button>\n </form>\n {% endif %}\n{% endfor %}\n
\nmodels.py
\nfrom django.db import models\nfrom django.contrib.auth.models import User\n# Create your models here.\n\nclass Category(models.Model):\n name = models.CharField(max_length=100)\n\n def __str__(self):\n return self.name\n\n class Meta:\n verbose_name = 'Category'\n verbose_name_plural = 'Categories'\n\n\nclass Book(models.Model):\n name = models.CharField(max_length=200)\n description = models.TextField()\n image = models.ImageField()\n price = models.IntegerField()\n category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)\n\n def __str__(self):\n return self.name\n\nclass OrderItem(models.Model):\n order_id = models.CharField(max_length=10)\n user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)\n book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True)\n\n def __str__(self):\n return self.user.username\n\nclass CartItem(models.Model):\n user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)\n book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True)\n\n def __str__(self):\n return self.user.username\n\n
\n","author":{"@type":"Person","name":"Arpit Linux"},"upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"In your searchall method, you are trying to filter category. But, as your Book
model have category
property which is object not a string. So, you have to go further inside it and compare the name
of that category
.
So:
\ndef searchall(request):\n getobject = request.GET['search']\n getcategory = request.GET['category']\n if getcategory == 'All':\n getcategory = \"\"\n search = Book.objects.filter(name__icontains=getobject, category__name__icontains=getcategory)\n oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)\n lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)\n hehe = CartItem.objects.filter(user=request.user)\n category = Category.objects.all()\n\n fianlprice = 0\n for item in hehe:\n fianlprice += item.book.price\n books = Book.objects.all()\n return render(request, 'main/search.html', {'books':search, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})\n
\nRefs on span relationship lookups(__
).
Reputation: 259
So I'm trying to make an search option where users can search via categories and name but only name worked, when i used icontains on category it just gave me an error but did not give me an error with name also categories just does not load in HTML. Categories are supposed to load in <select>
tag but it is not working, searchall
function handles the page I'm talking about and search.html
is the page.
views.py
def searchall(request):
getobject = request.GET['search']
getcategory = request.GET['category']
if getcategory == 'All':
getcategory = ""
search = Book.objects.filter(name__icontains=getobject, category__icontains=getcategory)
oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
hehe = CartItem.objects.filter(user=request.user)
category = Category.objects.all()
fianlprice = 0
for item in hehe:
fianlprice += item.book.price
books = Book.objects.all()
return render(request, 'main/search.html', {'books':search, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})
search.html
<h1>search</h1>
<h1>{{ error }}</h1>
<h1>Your cart currently costs ${{ price }}</h1>
<form method="GET" action="">
<input type="text" placeholder="Search here" name="search" id="search">
<button type="submit">Search</button>
</form>
{% for book in books %}
<h3>{{ book.name }}</h3>
<img src= "/media/{{ book.image }}" alt="">
<p>{{ book.description }}</p>
{% if book.id in cart %}
<form method="POST" action="/removefromcartforproducts/">
{% csrf_token %}
<button type="submit" name="removeid" value="{{ book.id }}">remove item from cart</button>
</form>
{% elif book.id in order %}
<h3>You already own this</h3>
{% else %}
<form method="POST" action="/addtocartforproducts/">
{% csrf_token %}
<button type="submit" name="bookid" value="{{ book.id }}">Add to cart</button>
</form>
{% endif %}
{% endfor %}
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
class Book(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
image = models.ImageField()
price = models.IntegerField()
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.name
class OrderItem(models.Model):
order_id = models.CharField(max_length=10)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.user.username
class CartItem(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.user.username
Upvotes: 2
Views: 1879
Reputation: 4095
In your searchall method, you are trying to filter category. But, as your Book
model have category
property which is object not a string. So, you have to go further inside it and compare the name
of that category
.
So:
def searchall(request):
getobject = request.GET['search']
getcategory = request.GET['category']
if getcategory == 'All':
getcategory = ""
search = Book.objects.filter(name__icontains=getobject, category__name__icontains=getcategory)
oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
hehe = CartItem.objects.filter(user=request.user)
category = Category.objects.all()
fianlprice = 0
for item in hehe:
fianlprice += item.book.price
books = Book.objects.all()
return render(request, 'main/search.html', {'books':search, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})
Refs on span relationship lookups(__
).
Upvotes: 1