0

Let's say that I had an array in the Python backend and wanted to pass it to the front end as json or a js array. How would I be able to do this using the Django framework?

This is what the template might look like:

<script type="text/javascript">
var array = {{djangoVariable}};
//use the array somehow
</script>

5 Answers 5

3

I try the answer, but the think that work by me was like:

in the view.py

from django.shortcuts import render_to_response
from django.utils import simplejson


def example_view(request):
   variable = {"myvar":[1, 2, 3]}
   render_to_response("example.html", simplejson.dumps(variable),
            context_instance=RequestContext(request))

And in the example.html

...
<script type="text/javascript">
        var myvar = "{{ myvar|safe }}";
</script>
...
Sign up to request clarification or add additional context in comments.

Comments

2

In Django:
from django.utils import simplejson
json = simplejson.dumps(YOUR_VARIABLE)

AND PASS "json" IN CONTEXT

IN JS:
var YOUR_JS_OBJECT = {{json|safe}};

Comments

1

Full view so you get the idea:

from django.shortcuts import render_to_response
from django.core import serializers

def my_view(request) :
    json_data = serializers.serialize('json', my_django_object)
    render_to_response('my_template.html', {'json_data' : json_data})

6 Comments

+1 for using django's serializers. They are really useful. I should use them more.
haha I've actually never used them before. I just did a cursory search of the django docs for "json encode". I figured django probably had a fancy way of doing things.
I remember learning about them when I first studied about django. But since them I always used simplejson or even 'str' for that kind of need.
Django's serializers only work for querysets. Any other type of data, and you need to use simplejson directly.
Daniel is right. About the performance, django uses a bundled version of simplejson, so there are no benefits of using simplejson directly.
|
0

Maybe you should read a django tutorial on how to pass variables from python to the template.

BUT

If your python list is a list of numbers, you could just use str or the module simplejson on it that it would be already a javascript array to use in your template.

For example:

in your views.py:

import simplejson
def example_view(request)
   variable = [1, 2, 3]
   return render(request, 'example.html', {'djangoVariable': simplejson.dumps(variable)})

1 Comment

In fact in python 2.6+, the standard module json is exactly the same as simple json
0

In your view:

js_variable = simplejson.dumps(django_variable)

and pass js_variable to your template in the normal way.

1 Comment

I have tried it but " is translated to &quot; : [&quot;microones&quot;, &quot;normal&quot;, &quot;large&quot;, &quot;xl&quot;] instead ["microones", ...

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.