【发布时间】:2021-09-02 13:03:12
【问题描述】:
我使用 Django 作为后端,使用 Postgres SQL 作为 DB,使用 HTML、CSS 和 Javascript 作为前端。我卡在过滤选项中,用户选择check 一个品牌并在模板中显示所选品牌列表。所以基本上是这样的:
每个类别都有多个规格。就像手机一样:
- 品牌
- 内存
- 只读存储器 等
到目前为止,我已经完成了列表过滤,但我想要复选框过滤。
代码在这里:
views.py
def product(request, data=None):
product = Product.objects.all()
if data == None:
proc = Product.objects.filter(category = 1)
elif data == 'OnePlus' or data == 'boAt' or data == 'Redmi' or data == 'realme':
proc = Product.objects.filter(category = 1).filter(brand = data)
return render(request, 'list/processor.html', {'product': product, 'proc':proc,})
product.html
<ul class="list-group">
<a style="text-decoration:none" href="{% url 'main:product' %}">
<li class="list-group-item d-flex justify-content-between align-items-center">
All
</li>
</a>
<a style="text-decoration:none" href="{% url 'main:productdata' 'OnePlus'%}">
<li class="list-group-item d-flex justify-content-between align-items-center">
OnePlus
</li>
.......
</ul>
我搜索了Django-Filter,但没有正确实施复选框过滤。 复选框过滤将如何完成,因为此过程需要太多时间。有没有什么简单的方法可以让所有特定的列都得到一个过滤器,例如。如果品牌名称LG 重复不止一次,查询会将它们过滤为一个并将它们附加到复选框过滤?
【问题讨论】:
标签: django django-models django-views django-filter django-template-filters