django-models - 我了解复选框可能会传递多个值以进行进一步过滤。如何使用同一列的两个值来过滤表
问题描述
那是我的html文件:
<form action="{% url 'search' %}">
<div>
<label for="manufacture">Производитель</label><br>
<input type="checkbox" id="manufacture" name="manufacture" value="Samsung">Samsung <br>
<input type="checkbox" id="manufacture" name="manufacture" value="Honor">Honor <br>
<input type="checkbox" id="manufacture" name="manufacture" value="Huawei">Huawei <br>
<input type="checkbox" id="manufacture" name="manufacture" value="Apple">Apple <br>
<input type="checkbox" id="manufacture" name="manufacture" value="Xiaomi">Xiaomi <br>
</div>
<input type="submit" value="Submit"class="btn btn-primary">
</form>
那是我的views.py:
def search(request):
queryset_list=Product.objects.order_by('-created')
if 'manufacture' in request.GET:
manufacture=request.GET['manufacture']
if manufacture:
queryset_list=queryset_list.filter(brand__iexact = manufacture)
context= {
products': queryset_list,
}
return render (request, 'cart_2/search.html', context)
当我同时检查三星和荣耀时,过滤只给我一个制造商。我想要两个。请帮忙
解决方案
你想要MultiValuesDict.getlist()
和__in
查找。
def search(request):
qs = Product.objects.order_by('-created')
manufactures = request.GET.getlist("manufacture", None)
if manufactures:
qs = qs.filter(brand__in=manufactures)
context= {
'products': qs,
}
return render (request, 'cart_2/search.html', context)
此外(不相关但)您当然希望将您的品牌作为一个独特的模型(从产品到品牌的 FK) - 这是非常基本的关系模型规范化,并且有很好的理由这样做。
推荐阅读
- python - 如何从大数组中提取具有相同索引的子数组
- authentication - 具有现有身份的 Razor 页面和具有自己身份验证的 api
- c++ - 从 LoadResource 返回的 Win32 DLGTEMPLATEEX 结构是否意味着用户可写?
- c# - 如何根据所选项目相乘
- python - 从 Pandas Timeseries Dataframe 中的多天中选择特定时间
- r - 增加选择输入的高度
- google-apps-script - 当谷歌电子表格的特定单元格变为“检查校准表”时,我需要一个脚本来自动发送电子邮件
- tensorflow - 如果实际正确类别的概率很小(
我需要关于训练模型的损失(categorical_crossentropy)的输入-目标组合的梯度。对于大多数示例,梯度看起来不错,但网络训练得越深入,出现的输入-目标组合越多,所有梯度都为零。为什么会这样,因为当概率绝对不同于目标时会发生这种情况?
我将网络定义如下:
X_train, Y
- javascript - 输入 addEventListener - 点击有效,onchange/oninput 无效
- regex - 匹配法语字符