Django过滤器未通过url传递值

时间:2019-04-30 20:39:46

标签: django filtering

我一直在学习本教程https://www.youtube.com/watch?v=vU0VeFN-abU,但是当我尝试通过表单传递值时,'?'之后没有值出现。我在某处有错字吗?

当我输入C34时,应该在网址中输入/?C34

相反,我只是得到:/?

我的代码:

#Urls.py
path('', SampleFliterView, name='SampleFliterView'),

#views.py
def SampleFliterView(request):
    qs = Sample.objects.all()
    sample_number_query = request.GET.get('sample_number')
    container_name_query = request.GET.get('container_name')

    if container_name_query != '' and container_name_query is not None:
        qs = qs.filter(sample_number__icontains=container_name_query)

    context = {
        'queryset': qs
    }
    return render(request, "container/filter.html", context)

#html template
<form class="" action="." method="GET">

  <div class="form-row">
    <div class="form-group col-12">
      <div class="input-group">
        <input class="form-control py-2 border-right-0 border " type="text" name="" value="" placeholder="Sample Number" name="sample_number"/>
        <span class="input-group-append">
          <div class="input-group-text bg-transparent"><i class="fa fa-search"></i></div>
        </span>
      </div>
    </div>
  </div>

  <div class="form-row">
    <div class="form-group col-12">
      <div class="input-group">
        <input class="form-control py-2 border-right-0 border " type="text" name="" value="" placeholder="Container Name" name="container_name"/>
        <span class="input-group-append">
          <div class="input-group-text bg-transparent"><i class="fa fa-search"></i></div>
        </span>
      </div>
    </div>
  </div>


    <button type="submit" class="btn btn-primary">Search</button>
  </form>

1 个答案:

答案 0 :(得分:1)

您两次输入了name=""。删除空白内容,它将按预期工作