按类别的过滤器

时间:2020-11-12 19:41:38

标签: 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):
        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()

这是我的Views.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)

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

2 个答案:

答案 0 :(得分:0)

在您的views.py

中尝试使用此过滤器
if categoryID:
        
  products=Product.objects.filter(category__name=categoryID)
        
else:
        products = Product.get_all_products()

答案 1 :(得分:0)

这是您问题的可能答案。我对您的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参数来选择过滤给定类别的产品。如果您想按品牌进行过滤,则想法是相同的。只是不要忘记在您的环境中添加品牌。