jquery-autocomplete does not work with my django app.

Posted by HWM-Rocker on Stack Overflow See other posts from Stack Overflow or by HWM-Rocker
Published on 2010-02-25T18:41:13Z Indexed on 2010/04/09 17:43 UTC
Read the original article Hit count: 387

Filed under:
|

Hi everybody,

I have a problem with the jquery-autocomplete pluging and my django script. I want an easy to use autocomplete plugin. And for what I see this (http://code.google.com/p/jquery-autocomplete/) one seems very usefull and easy. For the django part I use this (http://code.google.com/p/django-ajax-selects/) I modified it a little, because the out put looked a little bit weired to me. It had 2 '\n' for each new line, and there was no Content-Length Header in the response. First I thought this could be the problem, because all the online examples I found had them. But that was not the problem.

I have a very small test.html with the following body:

<body>
<form action="" method="post"> 
<p><label for="id_tag_list">Tag list:</label> 
<input id="id_tag_list" name="tag_list" maxlength="200" type="text" /> </p> 
<input type="submit" value="Submit" /> 
</form> 
</body>

And this is the JQuery call to add autocomplete to the input.

function formatItem_tag_list(bla,row) {
    return row[2]
}
function formatResult_tag_list(bla,row) {
    return row[1]
}

$(document).ready(function(){
    $("input[id='id_tag_list']").autocomplete({
        url:'http://gladis.org/ajax/tag',
        formatItem: formatItem_tag_list,
        formatResult: formatResult_tag_list,
            dataType:'text'
    }); 
});

When I'm typing something inside the Textfield Firefox (firebug) and Chromium-browser indicates that ther is an ajax call but with no response. If I just copy the line into my browser, I can see the the response. (this issue is solved, it was a safety feature from ajax not to get data from another domain)

For example when I am typing Bi in the textfield, the url "http://gladis.org/ajax/tag?q=Bi&max... is generated. When you enter this in your browser you get this response:

4|Bier|Bier
43|Kolumbien|Kolumbien
33|Namibia|Namibia

Now my ajax call get the correct response, but there is still no list showing up with all the possible entries. I tried also to format the output, but this doesn't work either. I set brakepoints to the function and realized that they won't be called at all.

Here is a link to my minimum HTML file http://gladis.org/media/input.html

Has anybody an idea what i did wrong. I also uploaded all the files as a small zip at http://gladis.org/media/example.zip.

Thank you for your help!

[Edit] here is the urls conf:

(r'^ajax/(?P<channel>[a-z]+)$', 'ajax_select.views.ajax_lookup'),

and the ajax lookup channel configuration

AJAX_LOOKUP_CHANNELS = {
    # the simplest case, pass a DICT with the model and field to search against :
    'tag' : dict(model='htags.Tag', search_field='text'),
}

and the view:

def ajax_lookup(request,channel):
    """ this view supplies results for both foreign keys and many to many fields """

    # it should come in as GET unless global $.ajaxSetup({type:"POST"}) has been set
    # in which case we'll support POST
    if request.method == "GET":
        # we could also insist on an ajax request
        if 'q' not in request.GET:
            return HttpResponse('')
        query = request.GET['q']
    else:
        if 'q' not in request.POST:
            return HttpResponse('') # suspicious
        query = request.POST['q']

    lookup_channel = get_lookup(channel)

    if query:
        instances = lookup_channel.get_query(query,request)
    else:
        instances = []

    results = []
    for item in instances:
        results.append(u"%s|%s|%s" % (item.pk,lookup_channel.format_item(item),lookup_channel.format_result(item)))

    ret_string = "\n".join(results)
    resp = HttpResponse(ret_string,mimetype="text/html")
    resp['Content-Length'] = len(ret_string)
    return resp

© Stack Overflow or respective owner

Related posts about jquery-autocomplete

Related posts about django