Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6790515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:44:03+00:00 2026-05-26T17:44:03+00:00

I’m trying to write a simple search server, using Django with AJAX. The server

  • 0

I’m trying to write a simple search server, using Django with AJAX.
The server itself works fine, but I am still struggling with adding autocomplete to the search widget.

(I don’t want to use available Django snippets, since they don’t do exactly what I want, are hard to customize, and don’t teach me the basics the way writing the interface from scratch does)

On the client (Javascript) side, I’m using YUI because it looks simpler – but don’t mind switching to jQuery, so please don’t fixate on that.

In YUI, an autocomplete box takes three parameters: input,container and dataSource.
The first two are just the widgets, and the third one is interesting.

If I write:

var oDS = new YAHOO.util.LocalDataSource(["apples","apples2", "broccoli", "cherries"]);
var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);

in my <script>, I get an autocomplete box which autocompletes these terms.

When I try to replaces the LocalDataSource with a remote datasource, for example by choosing var oDS = new YAHOO.util.ScriptNodeDataSource("127.0.0.1:8000/getNames/"), and setting up an apropriate view, no autocomplete happens.

What I know about the problem:

  1. I know that the view gets called (by debug printing) – so that isn’t the problem.
  2. I return a json array by jresponse = simplejson.dumps(response_array); return HttpResponse(jresponse, mimetype='application/javascript');
    I don’t think there’s any problem with that, since I can access that view directly and get a textual representation of the json when I do.
  3. Maybe there’s a problem with the input-type that the local data source expects – I’m not sure how to set it.

Any help (including how to do this correctly in jQuery – I don’t mind dumping YUI) would be much appreciated.


Edit: After the first two comments (thanks!), I installed firebug, and started playing with it. It’s really messy, however, since the Yahoo toolbar has lots of code, and I don’t know where to break it. Is there some easy way to have firebug only show me json/xml elements?

@Marat: I did that already (accessing my view), and it returns a string representation of the JSON (at least that’s what my browser shows). Is that what I should expect?

@pastylegs: Here’s the code:

 <div id="myAutoComplete">
    <input id="myInput" type="text">
    <div id="myContainer"></div>
    </br></br>
    <input type="submit">
</div>

 <script type="text/javascript">
YAHOO.example.BasicLocal = function() {
{%block oDS%}
// Use a LocalDataSource
var oDS = new YAHOO.util.LocalDataSource(["apples","apples2", "broccoli", cherries"]);
//for remote - oDS = new YAHOO.util.ScriptNodeDataSource("127.0.0.1:8000/getNames/")


// Instantiate the AutoComplete
var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
oAC.prehighlightClassName = "yui-ac-prehighlight";
oAC.useShadow = true;

return {
    oDS: oDS,
    oAC: oAC
};
}();
</script>

The Django view:

def getNamesXML(request):
    response_array=['Apples','Oranges']
    print response_array
    jresponse = simplejson.dumps(response_array)
    print jresponse
    return HttpResponse(jresponse, mimetype='application/javascript')

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:44:04+00:00Added an answer on May 26, 2026 at 5:44 pm

    I don’t know anything about YUI, but I can give you a working jQuery example. Main difference I see with your code is the mimetype: as discussed in this question, you should use application/json. That said, I don’t think it’ll make a big difference.

    For the view, use something along these lines:

    def autosuggest(request):
        query = request.GET.get('term') # jQuery autosuggest input so far
        f = MyModel.objects.filter(name__icontains=query)
        response = [p[0] for p in f.order_by("name")[:10].values_list("name")]
        return HttpResponse(simplejson.dumps(response), mimetype="application/json")
    

    This assumes f is a QuerySet on a model that has a name field, and you want the first 10 matches. In your template, the following should do:

    $('#myInput').autocomplete({
        source: '{% url myapp.views.autosuggest %}',
        minLength: 2, // Two characters are needed before suggestions show
        select: function(event, ui) { // Callback function for selection
            $('#myInput').val(ui.item.value);
        },
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.