gjivanya
gjivanya

Reputation: 589

python functions call in javascript autocomplete

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?

Upvotes: 2

Views: 421

Answers (2)

David Gleba
David Gleba

Reputation: 537

I was getting this same error.

It worked when the javascript was in the html file ( base.html in my case), but not when it was in a separate file I called dgmain.js.

I put the javascript in my template (create_product.html) in the {% block scripts %} and it works there.

It seems like it needs to be in the template, not in a seperate .js file when jinja2 is being used.

Upvotes: 0

Wondercricket
Wondercricket

Reputation: 7882

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'
})

Upvotes: 1

Related Questions