django - 搜索页面代码

1. 搜索手机号页面代码 html 页面代码 

 <form class="navbar-form navbar-right" method="get">
                <div class="form-group">
                  <input type="text" name="q" class="form-control" placeholder="Search" value={{ value }}>
                </div>
                <button type="submit" class="btn btn-default">搜索</button>
 </form>

2. views 代码 与 查询代码 相结合 

def number_list(request):
    # 查询 电话号码 mobile
    data_dict = {}
    value = request.GET.get('q', '')
    if value:
        data_dict['mobile__contains'] = value
    response = models.Number.objects.filter(**data_dict)
    return render(request, 'number_list.html', {'response': response, 'value': value})

 3. 搜索相关

  1. 多字段搜索 

1.  普通字段查询方式   order_by('-price') 排序 
models.表名.objects.filter( 字段 = "字段", 字段 = "字段").order_by('-price')

2.  字典查询
字典 = { 字段 = "字段", 字段 = "字段"  }
models.表名.objects.filter(**字典)

3.  以 字符串 999 开头 来查询 
models.表名.objects.filter(字段__startswith='999')

4.  以字符 999 串结尾来查询
models.表名.objects.filter(字段__endswith='999')

5.  包含 999 字符串来查询
models.表名.objects.filter(字段__contains='999')

字典 = { '字段__contains'  =  '999' }
models.表名.objects.filter(**字典)

  2. 数字搜索 

models.表名.objects.filter(id = 1)      # id = 1
models.表名.objects.filter(id__gt=1)     # id 大于 1
models.表名.objects.filter(id__gte=1)   # id 大于等于 1
models.表名.objects.filter(id__lt=1)     # id 小于 1
models.表名.objects.filter(id__lte=1)    # id 小于等于 1

 

posted @ 2022-05-27 17:19  我在春天等伱  阅读(53)  评论(0)    收藏  举报