0

using Django 1.3: from django view i'm trying to pass a dictionary

DATA = {'class1': {'age': 32,
                  'count': 1},
         'class2': {'age': 43,
                   'count': 5},
         'class3': {'age': 32,
                     'count': 10}
}

from view

render(request, "home.html", {'DATA': json.dumps(DATA)})

in javascript , trying to get that dictionary but

<script type="text/javascript">

                var data = "{{ DATA }}"
            </script>

data is coming like :

"{&quot;unlimited&quot;: {&quot;price&quot;: 99.99, &quot;limit&quot;: 10000}, &quot;premium&quot;: {&quot;price&quot;: 59.99, &quot;limit&quot;: 10}, &quot;free&quot;: {&quot;price&quot;: 0, &quot;limit&quot;: 1}, &quot;basic&quot;: {&quot;price&quot;: 39.99, &quot;limit&quot;: 5}}"

have tried safe and escape filter, template error : DATA could not be parsed.

how to access DATA in javascript correctly ?

navyad
  • 3,752
  • 7
  • 47
  • 88

1 Answers1

0

I assume you are trying to return json so:

import json
from django.http import HttpResponse

def some_method(requset):
    ...
    DATA = {...}

    return HttpResponse(json.dumps(DATA), content_type='application/json')

As far as decoding the json refer to this answer https://stackoverflow.com/a/4935684/785808

Community
  • 1
  • 1
Ernest Jumbe
  • 758
  • 9
  • 21