0

I'm implementing an autocomplete search using jQuery UI and Django models.

Here is my view:

def get_ticker(request):
    if request.is_ajax():
        q = request.GET.get('term', '')
        symbols = Symbol.objects.filter(name__istartswith=q)
        results = []
        for symbol in symbols:
            symbol_json = {'name': symbol.name, 'symbol': symbol.symbol}
            results.append(symbol_json)
        data = json.dumps(results)
    else:
        data = 'Can\'t get data...'
    mimetype = 'application/json'
return HttpResponse(data, mimetype)

url:

url(r'^api/get_ticker/$', views.get_ticker, name='get_ticker'),

js:

$(function() {
   $("#ticker").autocomplete({
      source: "{% url get_ticker %}",
      minLength: 2,
   });
});

html:

 <div class="ui-widget">
      <input type="text" id="ticker">
 </div>

When I type in search box, I get error:

GET http://127.0.0.1:8000/finance/%7B%%20url%20get_ticker%20%%7D?term=hello 404 (NOT FOUND)
3
  • Is the js served as static content or is it rendered? You need to render it (or put the url in the html file, which is probably a better idea). Commented Jun 3, 2015 at 16:54
  • it's is static content. How do rendered it?@noamk Commented Jun 3, 2015 at 16:58
  • There are ways to render static django content (you can find them using google), but that's not a good solution IMHO. This javascript file is served as-is, without changes. The proper way to do this is to put the url in some property in the html (which is rendered), and access the property from the javascript. Commented Jun 3, 2015 at 19:57

3 Answers 3

1

I suspect you're serving the JS file separately. If that's the case, then you'll have to:

either write the full URL yourself in the JS file.

source: "/api/get_ticker/",

or copy the script in your HTML template.

<script>
    $(function() {
       $("#ticker").autocomplete({
           source: "{% url 'get_ticker' %}",
           minLength: 2,
       });
   });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

I think search is working but when I type in, it shows me drop down list without any text. Any ideas?
0

Try this:

$(function(){
  $('#ticker').autocomplete({
    source: "{% url 'get_ticker' %}",
    minLength: 2,
  });
});

2 Comments

I'm still getting 404 (NOT FOUND).
Are your url and application registered?
0

Replace:

 "{% url get_ticker %}"

with

 "{% url 'get_ticker' %}"

Make note of the quotes

3 Comments

I'm still getting 404 (NOT FOUND).
Yes exactly the same.@IanAuld
Try "{% url 'get_ticker' | safe %}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.