Python 铁路超高按类别过滤
我正在尝试建立一个分类过滤系统。每当我尝试单击我的一个类别时,它总是显示所有产品,但我想显示过滤类别列表。如果我单击智能手机,它将仅显示智能手机类别产品 这是我的模型。Py:Python 铁路超高按类别过滤,python,django,Python,Django,我正在尝试建立一个分类过滤系统。每当我尝试单击我的一个类别时,它总是显示所有产品,但我想显示过滤类别列表。如果我单击智能手机,它将仅显示智能手机类别产品 这是我的模型。Py: from django.db import models # Create your models here. class Category(models.Model): name = models.CharField(max_length=100) def __str__(self):
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
通过创建一个唯一的slug字段,您就创建了一个索引。现在让我们看看修改后的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参数,可以根据给定的类别选择性地筛选产品。如果您想按品牌筛选,想法也是一样的。只是不要忘记在您的上下文中添加一个品牌。您如何准确构建到按类别筛选的视图的链接。