欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

今天的Django教训汇总:建立查询页面最好只用一个form-20211020

程序员文章站 2022-06-20 20:29:24
...
  • 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>

今天的Django教训汇总:建立查询页面最好只用一个form-20211020

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={})