django搜索自动完成无法正常工作

时间:2016-07-20 06:11:14

标签: python django

我正在尝试使用自动完成功能进行搜索,但我收到此错误“Uncaught TypeError:$(...)。autocomplete不是函数”。我已经包含了jquery库

<head>
    <link rel="stylesheet" href="{% static 'styles.css' %}">
    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script src="{% static 'script.js' %}"></script>
    <title>{% block title %}Home{% endblock %}</title>   
     <script>
        $(document).ready(function() {
            $( "#search_text" ).autocomplete({
               source: "/item_search/",
               minLength: 2,
            });
         });
      </script>
</head>
<body>
    <div id='cssmenu'>
        <ul>
            <li class='menus'><a href="{% url 'welcome_user' %}"><span>Home</span></a></li>
            <li class='menus'><a href="{% url 'user_logout' %}"><span>Logout</span></a></li>
            <li id="input" style="padding:11px;float:right;">  
                <form name="myform" method="GET" action="{% url 'search' %}">
                    <input type="text" name="search" id="search_text" placeholder="Search"/>
                </form>   
        </ul>
    </div>

views.py,

def item_search(request):
    book_name =  request.GET.get('search')       
    if book_name is not None:  
        rows = Add_prod.objects.filter(book__icontains=book_name) 
        results = [ x.book for x in rows ] 
    return HttpResponse(json.dumps(results))

2 个答案:

答案 0 :(得分:0)

要使自动完成工作,您需要在头脑中包含jQuery UI库。 你可以从这里下载 - &gt; https://jqueryui.com/download/all/

答案 1 :(得分:0)

实际上我认为你正在使用两个jquery删除这个

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>