Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango and AJAX - what am I doing wrong?
    text
    copied!<p>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.</p> <p>(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)</p> <p>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.</p> <p>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.</p> <p>If I write:</p> <pre><code>var oDS = new YAHOO.util.LocalDataSource(["apples","apples2", "broccoli", "cherries"]); var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS); </code></pre> <p>in my <code>&lt;script&gt;</code>, I get an autocomplete box which autocompletes these terms.</p> <p>When I try to replaces the LocalDataSource with a remote datasource, for example by choosing <code>var oDS = new YAHOO.util.ScriptNodeDataSource("127.0.0.1:8000/getNames/")</code>, and setting up an apropriate view, no autocomplete happens.</p> <p>What I know about the problem:</p> <ol> <li>I know that the view gets called (by debug printing) - so that isn't the problem.</li> <li>I return a json array by <code>jresponse = simplejson.dumps(response_array); return HttpResponse(jresponse, mimetype='application/javascript');</code> 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.</li> <li>Maybe there's a problem with the input-type that the local data source expects - I'm not sure how to set it.</li> </ol> <p>Any help (including how to do this correctly in jQuery - I don't mind dumping YUI) would be much appreciated.</p> <hr> <p>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?</p> <p>@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?</p> <p>@pastylegs: Here's the code:</p> <pre><code> &lt;div id="myAutoComplete"&gt; &lt;input id="myInput" type="text"&gt; &lt;div id="myContainer"&gt;&lt;/div&gt; &lt;/br&gt;&lt;/br&gt; &lt;input type="submit"&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; 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 }; }(); &lt;/script&gt; </code></pre> <p>The Django view:</p> <pre><code>def getNamesXML(request): response_array=['Apples','Oranges'] print response_array jresponse = simplejson.dumps(response_array) print jresponse return HttpResponse(jresponse, mimetype='application/javascript') </code></pre> <p>Thanks a lot!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload