如何在 Django 中对搜索结果项进行排序
how to do sorting of a search result item in django
我有搜索字段,我想为用户提供排序选项,以便用户可以在搜索结果中按从低到高到低的价格进行排序,但无法实现
我的观点
class search_item(View):
def get(self, request):
search_item = request.GET.get('search')
if search_item:
items = Item.objects.filter(Q(name__icontains=search_item)|Q(color__icontains=search_item))
sort_by = request.GET.get("sort", "l2h")
if sort_by == "l2h":
item = items.order_by("price")
elif sort_by == "h2l":
item = items.order_by("-price")
return render(request, 'search_result.html', {'items':item})
它执行搜索但无法对我的代码中的错误进行排序
html
<li><a class="dropdown-item" href="?sort=l2h">Low To High</a></li>
<li><a class="dropdown-item" href="?sort=h2l">High To Low</a></li>
我的模型
class Item(models.Model):
categories = models.ForeignKey(Categories, on_delete=models.CASCADE, related_name='our_items')
subcategories = models.ForeignKey(Subcategories, on_delete=models.CASCADE, related_name='products')
name = models.CharField(max_length=200, blank=False)
swag = models.BooleanField(blank=False, default=False)
male = models.BooleanField(blank=False, default=False)
female = models.BooleanField(blank=False, default=False)
unisex = models.BooleanField(blank=False, default=False)
first = models.ImageField(upload_to='items', blank=False)
second = models.ImageField(upload_to='items', blank=False)
third = models.ImageField(upload_to='items', blank=True)
color = models.CharField(max_length=30, blank=False, default='Black')
material = models.CharField(max_length=50, blank=False, default='Plastic' )
price = models.FloatField(blank=False,)
description = models.TextField(blank=True)
@staticmethod
def get_items_by_id(ids):
return Item.objects.filter(id__in = ids)
def __str__(self):
return self.name
我的url
path('search-result/',search_item.as_view(),name='search')
按重新使用的商品价格排序时出错
视图 products.views.search_item 没有 return HttpResponse 对象。它 return 改为 None。
回溯
Traceback (most recent call last):
File "C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\base.py", line 188, in _get_response
self.check_response(response, callback)
File "C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\base.py", line 309, in check_response
raise ValueError(
Exception Type: ValueError at /search-result/
Exception Value: The view products.views.search_item didn't return an HttpResponse object. It returned None instead.
查看排序工作完美无缺的视图
class Product(View):
def get(self, request, subcategory_id):
subcategory = get_object_or_404(Subcategories, pk=subcategory_id)
sort_by = request.GET.get("sort", "l2h")
if sort_by == "l2h":
products = subcategory.products.order_by("price")
elif sort_by == "h2l":
products = subcategory.products.order_by("-price")
sort_by = request.GET.get("sort", "newest")
if sort_by == "unisex":
products = subcategory.products.order_by("-unisex")
elif sort_by == "newest":
products = subcategory.products.order_by("-update_at")
sort_by = request.GET.get("sort", "male")
if sort_by == "male":
products = subcategory.products.order_by("-male")
elif sort_by == "female":
products = subcategory.products.order_by("-female")
category_list = Categories.objects.all()
return render (request, 'products.html',{"subcategory_list" : products, 'category_list': category_list })
以下错误信息是关键:
products.views.search_item didn't return an HttpResponse object. It returned None instead.
它告诉你,你的 products.views.search_item
方法 returned None。怎么可能returnNone?它 returns None,隐含地,当 search_item
有一个假值时,它完全跳过项目的 reading/sorting。当 search
参数不存在(或为空)时会发生这种情况。
我有搜索字段,我想为用户提供排序选项,以便用户可以在搜索结果中按从低到高到低的价格进行排序,但无法实现 我的观点
class search_item(View):
def get(self, request):
search_item = request.GET.get('search')
if search_item:
items = Item.objects.filter(Q(name__icontains=search_item)|Q(color__icontains=search_item))
sort_by = request.GET.get("sort", "l2h")
if sort_by == "l2h":
item = items.order_by("price")
elif sort_by == "h2l":
item = items.order_by("-price")
return render(request, 'search_result.html', {'items':item})
它执行搜索但无法对我的代码中的错误进行排序
html
<li><a class="dropdown-item" href="?sort=l2h">Low To High</a></li>
<li><a class="dropdown-item" href="?sort=h2l">High To Low</a></li>
我的模型
class Item(models.Model):
categories = models.ForeignKey(Categories, on_delete=models.CASCADE, related_name='our_items')
subcategories = models.ForeignKey(Subcategories, on_delete=models.CASCADE, related_name='products')
name = models.CharField(max_length=200, blank=False)
swag = models.BooleanField(blank=False, default=False)
male = models.BooleanField(blank=False, default=False)
female = models.BooleanField(blank=False, default=False)
unisex = models.BooleanField(blank=False, default=False)
first = models.ImageField(upload_to='items', blank=False)
second = models.ImageField(upload_to='items', blank=False)
third = models.ImageField(upload_to='items', blank=True)
color = models.CharField(max_length=30, blank=False, default='Black')
material = models.CharField(max_length=50, blank=False, default='Plastic' )
price = models.FloatField(blank=False,)
description = models.TextField(blank=True)
@staticmethod
def get_items_by_id(ids):
return Item.objects.filter(id__in = ids)
def __str__(self):
return self.name
我的url
path('search-result/',search_item.as_view(),name='search')
按重新使用的商品价格排序时出错 视图 products.views.search_item 没有 return HttpResponse 对象。它 return 改为 None。
回溯
Traceback (most recent call last):
File "C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\base.py", line 188, in _get_response
self.check_response(response, callback)
File "C:\Users\mithlesh\Desktop\coolbuy2.0\lib\site-packages\django\core\handlers\base.py", line 309, in check_response
raise ValueError(
Exception Type: ValueError at /search-result/
Exception Value: The view products.views.search_item didn't return an HttpResponse object. It returned None instead.
查看排序工作完美无缺的视图
class Product(View):
def get(self, request, subcategory_id):
subcategory = get_object_or_404(Subcategories, pk=subcategory_id)
sort_by = request.GET.get("sort", "l2h")
if sort_by == "l2h":
products = subcategory.products.order_by("price")
elif sort_by == "h2l":
products = subcategory.products.order_by("-price")
sort_by = request.GET.get("sort", "newest")
if sort_by == "unisex":
products = subcategory.products.order_by("-unisex")
elif sort_by == "newest":
products = subcategory.products.order_by("-update_at")
sort_by = request.GET.get("sort", "male")
if sort_by == "male":
products = subcategory.products.order_by("-male")
elif sort_by == "female":
products = subcategory.products.order_by("-female")
category_list = Categories.objects.all()
return render (request, 'products.html',{"subcategory_list" : products, 'category_list': category_list })
以下错误信息是关键:
products.views.search_item didn't return an HttpResponse object. It returned None instead.
它告诉你,你的 products.views.search_item
方法 returned None。怎么可能returnNone?它 returns None,隐含地,当 search_item
有一个假值时,它完全跳过项目的 reading/sorting。当 search
参数不存在(或为空)时会发生这种情况。