简体   繁体   English

django搜索自动完成不起作用

[英]django search autocomplete not working

I am trying to use autocomplete for search but I am getting this error "Uncaught TypeError: $(...).autocomplete is not a function".I have already included jquery library 我正在尝试使用自动完成功能进行搜索,但出现此错误“ 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, 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))

For autocomplete to work, you need to have have jQuery UI library included in your head. 为了使自动完成工作正常运行,您需要将jQuery UI库包含在脑海中。 You can download it from here -> https://jqueryui.com/download/all/ 您可以从这里-> https://jqueryui.com/download/all/下载

实际上我认为您正在使用两个jquery删除此一个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM