I’m trying to make an array out of a text file My django/python code is
f = open('text file path here', 'r')
names = []
for line in f:
names.append(line)
f.close()
return render_to_response('frontend.html', {'names'}, context_instance=RequestContext(request))
Then my frontend.html takes the array, names, and uses the data for autocomplete in a form whose id=’status’:
<script>
var names = {{ names }};
$(document).ready(function(){
$("#status").autocomplete(names);
});
</script>
Nothing happens in the field on frontend.html when I start typing someone’s name. Any thoughts?
You can build an url endpoint inside your
url.pyand in your views you can return a response withnamesin JSON and fetch them in your template using a getJSON call.View
Template
You need to set the endpoint in your URLConf https://docs.djangoproject.com/en/1.3/topics/http/urls/
See here for a complete example: http://mitchfournier.com/2011/06/06/getting-started-with-ajax-in-django-a-simple-jquery-approach/