1

I'm send a list from Django(Python) to jQuery, but it is throwing some error while receiving it?

Here is what I send in python.

hubcode_list = ['a','b']
ctx = {'form':form, 'hubcode_list' : hubcode_list, 'admin_permission' : admin_permission}
return render(request, 'dummy/config.html', ctx)

jQuery:-

var hubcode_list = {{ hubcode_list }}

It returns an error saying :-

SyntaxError: syntax error
var hubcode_list = ['a', 'b']

When I use escapejs as

var hubcode_list = {{ hubcode_list|escapejs }}

it throws

SyntaxError: illegal character
var hubcode_list = [\u0027a\u0027, \u0027b\u0027]

How do I receive a list from server(Python)? I need to implement an autocomplete functionality where I need to pass a list of tags.

$( ".hubcode_list" ).autocomplete({
        source: hubcode_list
    });

4 Answers 4

3
hubcode_list = [str(i) for i in hubcode_list]  #unicode string not understood by javascript
{{ hubcode_list|safe }}

The good way is to pass json.

>>> import json
>>> hubcode_list = ['a','b']
>>> json.dumps(hubcode_list)
>>> '["a", "b"]'

jQuery:

var hubcode_list = JSON.parse(hubcode_list)

If you want to implement jquery Autocomplete, you should implement it via AJAX. You shouldn't pass all the autocomplete at once. What if there are thousands of data? See this tutorial on how to implement jquery autocomplete with Django.

Sign up to request clarification or add additional context in comments.

5 Comments

Throws the following error SyntaxError: missing ] after element list var hubcode_list = [u'KOLS', u'ASN']
Now this error. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data var hubcode_list = JSON.parse(hubcode_list)
But I just dump hubcode_list not everything. ie, hubcode_list = json.dumps(['a','b']) ctx = {'form':form, 'hubcode_list' : hubcode_list, 'admin_permission' : admin_permission} return render(request, 'dummy/config.html', ctx)
Your second comment shows the JSON is invalid. What is the actual data on hubcode_list variable.
Actual data is ['ASN','MUM','DEL']
0

You can use SafeString

from django.utils.safestring import SafeString
...
return render(request, 'dummy/config.html', {'ctx': SafeString(ctx)})

But ctx needs to be a valid json format variable

Comments

0

Some of the other answers are on the right track, but aren't clear enough.

To pass data from Python to Javascript, use JSON.

hubcode_list = json.dumps(['a','b'])

1 Comment

I tried but i keep getting different errors. Dont know the correct way of doing it. I tried with your code as well.
0

Did you tried something like:

import json
ctx = {'form':form, 'hubcode_list' : hubcode_list, 'hubcode_json': json.dumps(hubcode_list), 'admin_permission' : admin_permission}
return render(request, 'dummy/config.html', ctx)

In your jQuery you can do something like

var parsed_json = eval("({{ hubcode_json }})");

or

var parsed_json = $.parseJSON("{{ hubcode_json }}");

Ok, is not the cleanest way but it works, at least, for me.

4 Comments

No, this is wrong. Why to convert the whole context to json?
Says syntax error. Dont know why? SyntaxError: syntax error (["a", "b"])
eval is evil. That's not the way to parse the json. Only use eval if you completely trust your data.
I normally trust my data when it goes to a template, my server part takes care about it ;) we are not talking about security, just about json right? :) anyway U can use something like $.parseJSON("{{ hubcode_json }}")

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.