gpt4 book ai didi

python - 在 Django 中过滤

转载 作者:太空宇宙 更新时间:2023-11-04 11:01:10 27 4
gpt4 key购买 nike

我正在尝试在从下拉列表中选择的选项和文本字段之间实现一个简单的过滤方法。下拉菜单有姓名、职务、员工 ID(这些都是数据库中的所有字段)等选项。有一个文本字段,可以在其中给出指定选择的值。例如:如果我从下拉列表中选择名称,并在文本字段中选择“软件工程师”:我想显示名称为软件工程师的用户。如果我从下拉列表中选择名称并在文本字段中给出“John”,我想显示名称为 John 的用户。我怎么能在 Django 中做到这一点。我对 Django 很陌生,所以请帮我做这件事。我会在这里粘贴我的代码。VIEWS.PY

def filter(request):
val=request.POST.get('designation')
val2=request.POST.get('choices')
val3=request.POST.get('textField')
print val
print val2,val3
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=request.POST.get('choices'))
print val2
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName=request.POST.get('choices'))
print val2
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=request.POST.get('choices'))
print val2
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=request.POST.get('choices'))
print val2
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=request.POST.get('choices'))
print val2
else:
print "Data Not Found"

return render_to_response('filter.html',{'newData':newData,'val2':val2})

HTML

<form action="http://10.1.0.90:8080/filter/" method="POST">
Filter By:
<select name="choices" onsubmit="document.forms[0].submit()" >
<option value="">Select A Choice</option>
<option value="Name">Name</option>
<option value="Designation" >Designation</option>
<option value="EmployeeID" >EmployeeID</option>
<option value="Project" >Project</option>
<option value="DateOfJoin" >Date Of Join</option>
</select>
<input type="text" name="textField">
<input type="submit" value="Go">
</form>
<table>
{%for emp in emp_list.object_list%}
<tr> <td><a href ="http://10.1.0.90:8080/singleEmployee/{{emp.id}} "> {{ emp.userName }} </a></td> </tr><td>
{%endfor%}
</table></h4>
{%for data in newData%}
{{ data.userName}}<br>
{%endfor%}

MODELS.PY

class EmployeeDetails(models.Model):
userName = models.CharField(max_length=200)
designation = models.CharField(max_length=200)
employeeID = models.IntegerField()
contactNumber = models.CharField(max_length=200)
project = models.CharField(max_length=200)
dateOfJoin=models.TextField()

最佳答案

我在你的另一篇文章中给出了解决方案:

first, make your choice values EXACTLY the field names you want to search.

<select name="choices" onsubmit="document.forms[0].submit()" >
<option value="">Select A Choice</option>
<option value="userName">Name</option>
<option value="designation" >Designation</option>
<option value="employeeID" >EmployeeID</option>
<option value="project" >Project</option>
<option value="dateOfJoin" >Date Of Join</option>
</select>

然后,在您看来,使用关键字扩展将 choices 给出的字符串扩展为关键字参数。

if request.GET.get('choices'):
# prevent query on field ''
newdata= EmployeeDetails.objects.filter(**{request.GET.get('choices'): \
request.GET.get('textField')})

# this is the equivalent of running
# EmployeeDetails.objects.filter(keyword=request.GET['textField'])

关于python - 在 Django 中过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5189721/

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