python functions call in javascript autocomplete
Question: this is my views.py

    
    
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/autocomplete')
    
    *def get_contacts():*
    
        cat_list = []
        cat_list = contact.objects.all()
        return dumps(cat_list)
    

and this is my js

function showDialog() {

    
    
    $("#dialog").dialog(
    
    {
        width: 600,
        height: 400,
    
    });
    
    $.ajax({
        url:'{{url_for("autocomplete")}}'
    }).done(function(data){
    $( "#search" ).autocomplete({
                source: data
            });
    });
    

}

when I'm trying to run this i give the following error

GET <http://127.0.0.1:8000/share_win/%7B%7Burl_for(%22autocomplete%22)%7D%7D>
404 (NOT FOUND)

Any idea?


Answer: Your issue is coming from here:

    
    
    $.ajax({
        url:'{{url_for("autocomplete")}}'
    })
    

The `url_for` method requires the name of the function for the desired route
rather than physical route.

To fix this, you can either do:

    
    
    $.ajax({
        url:'{{url_for("get_contacts")}}'
    })
    

or if your Javascript is in a JS file, then do the following:

    
    
    $.ajax({
        url:'/autocomplete'
    })
    

