Lost in Django and jQuery UI Autofill Earth

I am trying to learn Django by following the tutorial in the old Django book, and I'm currently stuck with a jquery ui autocomplete implementation for a single field in my application. I searched a lot for this problem just to confuse me. So here is this code.

urls.py

`(r'^ajax/tag/autocomplete/$', ajax_tag_autocomplete),` 

tag_autocomplete.js

 $(document).ready(function () { $("#id_tags").autocomplete({ source: "/ajax/tag/autocomplete/", }); }); 

views.py

 def ajax_tag_autocomplete(request): if 'term' in request.GET: tags = Tag.objects.filter( name__istartswith=request.GET['term'] )[:10] return HttpResponse(u'\n'.join(tag.name for tag in tags)) return HttpResponse() 

Therefore, when I browse my application and start typing characters in this tag field, I don’t see anything, the list is not displayed. However, firebug shows me that the code is executing the request and returning the correct result. What simple thing am I missing?

+4
source share
3 answers

As Minars said in his comment, your view should return an array of strings or JSON objects to populate the data.

 from django.utils import simplejson 

returns json array

 return HttpResponse( simplejson.dumps( [ tag.name for tag in tags ] ) ) 
+6
source

Do you have tags entered in your database? Any results for the next?

 Tag.objects.all() 

Edit: Actually, instead of using the β€œterm” as the search key, try using β€œq”.

0
source

czarchaic answer is correct but dated.

simplejson excluded since Django 1.5

https://docs.djangoproject.com/en/1.7/releases/1.5/#django-utils-simplejson

instead of from django.utils import simplejson

using import json

0
source

Source: https://habr.com/ru/post/1384772/


All Articles