按类别筛选铁路超高



我正在尝试按类别创建一个过滤系统。每当我试图点击我的一个类别时,它总是显示所有产品,但我想显示筛选类别列表。如果我点击智能手机,它只会显示我的智能手机类别产品。

这是我的模型。Py:

from django.db import models
# Create your models here.

class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
@staticmethod   
def get_categories():
return Category.objects.all()

class Brand(models.Model):
name= models.CharField(max_length=100)
def __str__(self):
return self.name
def get_brands():
return Brand.objects.all()  
class Product(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.CASCADE, default='UNCATEGORIZED')
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, default='NoBrand')
price = models.FloatField()

@staticmethod
def get_all_products():
return Product.objects.all()

@staticmethod
def get_products_by_category(category_id):
if category_id:
return Product.objects.filter(category=category_id)
else:
return Product.get_all_products()

这是我的观点.py:

from django.shortcuts import render
from .models import *
# Create your views here.

def index(request):
products = None
cats = Category.get_categories()
brands = Brand.get_brands()
categoryID = request.GET.get('category')
if categoryID:
products = Product.get_products_by_category(categoryID)
else:
products = Product.get_all_products()
args = {
'products':products,
'cats': cats,
'brands': brands
}
return render(request, 'Home/index.html', args)

请帮帮我,我在这里很困惑,也被卡住了:(

在您的视图中尝试此过滤器.py

if categoryID:

products=Product.objects.filter(category__name=categoryID)

else:
products = Product.get_all_products()

以下是您的问题的可能答案。我稍微修改了你的models.py和views.py文件。结果如下:

from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100)

slug = models.SlugField(max_length=100, unique=True)

class Meta:
ordering = ('name',)
verbose_name = 'cat'
verbose_name_plural = 'cats'

def __str__(self):
return self.name
class Brand(models.Model):
name = models.CharField(max_length=100)

slug = models.SlugField(max_length=100, unique=True)

class Meta:
ordering = ('name',)
verbose_name = 'brand'
verbose_name_plural = 'brands'

def __str__(self):
return self.name

class Product(models.Model):
name = models.CharField(max_length=100, db_index=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, default='UNCATEGORIZED')
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, default='NoBrand')
price = models.FloatField()
slug = models.SlugField(max_length=100, db_index=True)

class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)

def __str__(self):
return self.name

通过创建一个唯一的段塞字段,您就是在创建一个索引。现在让我们看看修改后的views.py文件:

from django.shortcuts import render, get_object_or_404
from .models import *
def product_list(request, cat_slug=None):
cat = None
cats = Category.objects.all()
brand = None
brands = Brand.objects.all()
if cat_slug:
cat = get_object_or_404(Category, slug=cat_slug)
products = products.filter(category=cat)

args = {'products': products,
'cats': cats,
'cat': cat,
'brands': brands
}
return render(request, 'Home/index.html', args)

我们使用可选的cat_slug参数来按给定类别选择性地过滤产品。如果你想按品牌过滤,这个想法是一样的。只是不要忘记在你的上下文中添加一个品牌。

最新更新