【发布时间】:2021-09-07 19:12:22
【问题描述】:
我使用forms.BooleanField 进行复选框过滤。我一直无法从数据库中获取数据。
含义:如果用户点击SAMSUNG 产品,数据应过滤所有SAMSUNG 产品。
好吧,我已经尝试从数据库中获取品牌列表,并且效果很好,但是当我单击特定品牌时,它不会过滤特定品牌。 (它只是刷新并显示相同的数据)
Code goes here:
forms.py
class BrandForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
brands = Product.objects.filter(category=1).values_list('brand', flat=True)
for brand in brands:
self.fields[f'{brand}'] = forms.BooleanField(label=f'{brand}', required=False)
views.py
def product(request):
product = Product.objects.all().order_by('-id')
formBrand = BrandForm()
return render(request, 'list/product.html',
{'product': product, 'formBrand':formBrand}
)
index.html
<form action="{% url 'main:product' %}" method="get">
{{ formBrand.as_p }}
<input type="submit" value="OK">
</form>
所有代码都应该实现什么?
编辑 1
models.py
class Product(models.Model):
name = models.CharField(max_length=1330)
title = models.CharField(max_length=1330)
image_src = models.URLField(max_length=1330,null=True, blank=True)
link_href = models.URLField(max_length=1330,null=True, blank=True)
brand = models.CharField(max_length = 1330, null=True, blank=True)
price = models.DecimalField(max_digits=15, decimal_places=2)
category = models.IntegerField(default=1, choices=PRODUCT_CHOICES)
created = models.DateTimeField(auto_now_add=True)
【问题讨论】:
-
你能分享这个
main:productdata的观点吗? -
GET 请求无法获取表单数据,改为使用 POST 方法
-
你好@Arjun,它只是
main:product
标签: django forms django-views django-forms django-templates