如何在Django中创建两个搜索栏?

时间:2019-11-10 22:04:38

标签: python django

我正在构建一个简单的应用程序,它将允许用户搜索给定的值。例如,用户搜索“行业”和“收入”。我想知道如何使两个搜索栏一起工作。 预先感谢。

views.py

def应用程序(请求):     all_details = Company_Details.objects.all()

query=request.GET.get("q")
if query:
    all_details=all_details.filter(
        Q(industry__icontains=query)|
        Q(employees_number__icontains=query)|
        Q(revenue__icontains=query)
        ).distinct()








paginator=Paginator(all_details,3)
page=request.GET.get('page')
try:
    all_details=paginator.page(page)
except PageNotAnInteger:
    all_details=paginator.page(1)
except EmptyPage:
    all_details=paginator.page(paginator.num_pages)


context={
    'all_details':all_details
}
return render(request,'icpapp/app.html',context)

app.html

<div class="search">

    <form method='GET'  action="">
        <input type="text"   name='q' placeholder="Industry " list="Industry" class="searchterm" value='{{request.GET.q}}' />
             <datalist id="Industry">
              <option value="software">
              <option value="Security">
            </datalist>

            <input type="text"   name='q' placeholder="revenue " list="revenue" class="searchterm" value='{{request.GET.q}}' />
             <datalist id="revenue">
              <option value="20M">
              <option value="50M">
            </datalist>



        <button type='submit' value='Search' class='dfg' />search</button>
    </form>

</div>

{% if request.GET.q%}


{% for Company_Details in all_details %}

<h1>{{ Company_Details.company_name}}</h1>
<h1>{{Company_Details.industry}}</h1>
<h1>{{Company_Details.revenue}}</h1>
<h1>{{Company_Details.employees_number}}</h1>

 {%endfor%}


{%else%}
<h3>start searching</h3>
{%endif%}

0 个答案:

没有答案