首页 > 解决方案 > 按 django 选择过滤?

问题描述

我想按类别过滤。但是,它不起作用。

views.py  
  def BytovyeTexnikiPylesosy(request):
    
        products = BytovyeTexniki.objects.filter(category='pylesosy')
    
    
        context = {
        'products': products,
        }
        return render(request, 'store/product.html',context)
models.py
TEXNIKI = (
    ('pylesosy','Пылесосы'),
    ('stiralki','Стиральные машины'),
    ('xolodilniki','Холодильники'),
    )

lass BytovyeTexniki(models.Model):
    name = models.CharField(max_length=200)
    category = models.CharField(max_length=300, choices=TEXNIKI)

    price = models.FloatField()
    image = models.ImageField(null=True, blank=True)

    def __str__(self):
        return self.name

    @property
    def imageURL(self):
        try:
            url = self.image.url
        except:
            url = ''
        return url

我正在尝试使用此代码。但是,它不起作用。我想按类别过滤。请帮忙?

标签: djangodjango-modelsdjango-viewsdjango-forms

解决方案


推荐阅读