[Fixed]-Django JQuery autocomplete showing No search results

1👍

jquery.js is a static file which is not preprocessed by django template engine. So you have to replace the {% url %} tag with the hard coded url:

$(document).ready(function(){
   $( "#tags" ).autocomplete({
                        source: "/get_restaurant/",
                        selectFirst: true,
                        minLength: 2
   });
});

Or somehow pass this url from template to js. For example:

<script>
    var restaurantAutocompleteUrl = "{% url 'get_restaurant' %}";
</script>
<script src="{% static 'registration/jquery.js' %}"></script>

And then in the JS file:

$(document).ready(function(){
   $( "#tags" ).autocomplete({
                        source: restaurantAutocompleteUrl,
                        selectFirst: true,
                        minLength: 2
   });
});

Another option is to wrap the JS binding code into the function and call this function from the template:

<script src="{% static 'registration/jquery.js' %}"></script>
<script>
    bindAutocomplete("#tags", "{% url 'get_restaurant' %}");
</script>

Leave a comment