gpt4 book ai didi

javascript - 如何在 django 中按列过滤表

转载 作者:行者123 更新时间:2023-12-03 02:55:04 25 4
gpt4 key购买 nike

我试图在 django 项目模板中过滤我的列目前我可以在表中搜索并显示具有该值的行但我想添加一个下拉菜单,该菜单会询问特定的列值,然后根据该值进行过滤

目前我的代码是

View .py

def user_profile(request):
q = request.GET.get('q','')
custom = Customer.objects.all()
if q:
custom1=custom.filter(name__contains=q)
custom2=custom.filter(Product__contains=q)
custom3=custom.filter(L3__contains=q)
custom4=custom.filter(Server_Type__contains=q)
custom5=custom.filter(Version__contains=q)
custom6=custom.filter(Status__contains=q)
custom7=custom.filter(PM__contains=q)
custom8=custom.filter(CDM__contains=q)
custom = custom1 | custom2 | custom3 | custom4 | custom5 | custom6 |
custom7 | custom8

这里的名称产品l3是我的模型中的字段,我想从中过滤

在我的 home.html

 <input type="text" class="text_field" id="search" placeholder="Search for..." required>
<input type="button" class="button" onclick="go()" value="go" id="submit">

我的js.js

function go(){
// alert("hellonikhar");
var q = document.getElementById("search").value;

window.open("/?q="+q,"_self");
}

我将文本框中输入的值存储在 var q 中并显示数据,但我想首先添加一个下拉列表,该下拉列表将选择字段,然后 q 应仅根据该列进行过滤

最佳答案

要添加动态下拉列表,请从后端发送列名称。

<select name="columns">
{% for column in columns %}
<option value="{{column.name}}">Column {{column.id}}: {{column.name}}</option>
{% endfor %}
</select>

关于javascript - 如何在 django 中按列过滤表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47668906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com