- search_form.html有两个form,会导致第二个form数据读到第一个form的q去。
所以最好只留一个form。
<html>
<head>
<title>search_form</title>
</head>
<body>
<form action="/PPDASH/search_results/" method="get">
<a>请输入工号</a>
<input type="text" name="q">
<a>请输入部门</a>
<input type="text" name="dept">
<input type="submit" value="Search">
</form>
<!--
<form action="/PPDASH/search_results/" method="get">
<a>请输入部门</a>
<input type="text" name="dept">
<input type="submit" value="Search">
</form>
-->
</body>
view.py
# 建立查询结果界面
def search_results(request):
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
employee_list = Employee.objects.filter(eid=q) #增加了切片[0]
return render(request, 'PPDASH/search_results.html',context={'employee_list': employee_list, 'query': q})
elif 'dept' in request.GET and request.GET['dept']:
dept = request.GET['dept']
print(dept)
employee_list = Employee.objects.filter(dept_code=dept) #增加了切片[0]
return render(request, 'PPDASH/search_results.html',context={'employee_list': employee_list, 'query': dept})
else:
return render(request, 'PPDASH/search_form.html',context={})