如何在html中创建搜索部分并在django中使用多个查询搜索视图

时间:2019-05-08 02:31:58

标签: python django

我正在尝试制作汽车销售网页(出于教育目的),我找到了进行搜索的方法,但是我想使其看起来更好,因为在很多有可能选择汽车类别的汽车销售页面中,燃料类型,是否损坏等。 我现在的搜索视图是这样:

    def search(request):
        if 'search_filter' in request.GET:
            search_filter = request.GET['search_filter']
            cars = [{
                'id': x.id,
                'driven': x.driven,
                'description': x.description,
                'price': x.price,
                'firstImage': x.carimage_set.first().image
            } for x in Car.objects.filter(description__icontains=search_filter)
            ]
            return JsonResponse({'data': cars})
        context = {'cars': Car.objects.all().order_by('price')}
        return render(request, 'cars/car_index.html', context)

现在,如果在描述中找到我要查找的内容,则执行搜索。我的目标是做到这一点,以便用户可以在输入框中输入要搜索的值,例如汽车类型,汽车品牌,年份等。

<!DOCTYPE html>
<html>
<body>
<form action="">
<select name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>

    <legend>Additional Info:</legend>
    Car Type:<br>
    <input type="text" name="car_type">
    <br>
    Car Make:<br>
    <input type="text" name="car_make">
    <br><br>
    <input type="submit" value="Search">
</form>
</body>
</html>

这是一种非常原始的形式,但这是我对如何使其看起来像的想法。

1 个答案:

答案 0 :(得分:1)

您可以使用Q objects

使用隐藏字段,以便您知道何时执行搜索。

<!DOCTYPE html>
<html>
<body>
<form method="get">
<select name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>

    <legend>Additional Info:</legend>
    Car Type:<br>
    <input type="text" name="car_type">
    <br>
    Car Make:<br>
    <input type="text" name="car_make">
    <br><br>
    <input type="hidden" name="search_filter" value="true">
    <input type="submit" value="Search">
</form>
</body>
</html>

您的视图将是这样的:

import operator

def search(request):
        if 'search_filter' in request.GET:  # set a hidden field (search_filter) so you know that you need to perform a search
            search_params = request.GET.dict()
            search_params.pop("search_filter")
            # for all search params which are not null, create a Q object
            # assuming that you provide the column name as search criteria.
            q_list = [Q(("{}__icontains".format(param), search_params[param])) for param in search_params if search_params[param] is not None]  

            queryset = Car.objects.filter(reduce(operator.and_, q_list))  # combine Q object using & operation
            cars = [{
                'id': x.id,
                'driven': x.driven,
                'description': x.description,
                'price': x.price,
                'firstImage': x.carimage_set.first().image
            } for x in queryset
            ]
            return JsonResponse({'data': cars})
        context = {'cars': Car.objects.all().order_by('price')}
        return render(request, 'cars/car_index.html', context)

有关更多选项和对过滤器的控制,可以使用django-filters