django中的简单搜索未显示结果

时间:2018-09-21 09:24:29

标签: django search django-rest-framework

请提供任何有关为何未显示结果的帮助。

views.py

def display_object(request):
    obje= ObjectName.objects.all().order_by('objectName')
    paginator = Paginator(obje, 3)
    page = request.GET.get('page')
    obj= paginator.get_page(page)
    query_list = ObjectName.objects.all() 
    query= request.GET.get("q")
    if query:
         query_list= query_list.filter(objectName__icontains = query)

    imp = {
        "Objects":obj,
    }
    return render(request,'Impact/displayObject.html',imp)

displayObject.html

<form method='GET' action=''>
                <input  name='q' type="text" value='{{ request.GET.q }}'  placeholder="Search..."/>
                <input type="submit" value="search"/>
            </form>

输出 output

1 个答案:

答案 0 :(得分:1)

您没有应用过滤器。

尝试一下:

def display_object(request):
    query= request.GET.get("q")

    obje= ObjectName.objects.order_by('objectName')
    if query:
        obje = obje.filter(objectName__icontains = query)  

    paginator = Paginator(obje, 3)
    page = request.GET.get('page')
    obj= paginator.get_page(page)

    imp = {
        "Objects": obj,
    }
    return render(request,'Impact/displayObject.html',imp)