Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting serialized json objects from django templates?

I need a clarification. If I for example do a view with a serialized object:

def sample(request):
    res = [{'name':'man'}]
    encoded = json.dumps(res)
    return render_to_response('sample/example.html',{'encoded':encoded} )

In my templates I pass:

{{encoded}}

Now from a python script can I do:

data = json.loads(urllib2.urlopen(url/to/site).read()

It says ValueError: No JSON object could be decoded. But isn't {{encoded}} a json object? And if so how would I get it?

Thank you

like image 845
Neeran Avatar asked Dec 28 '22 06:12

Neeran


1 Answers

Try this in your template:

{% autoescape off %} 
  {{ encoded }}
{% endautoescape %} 
like image 82
Blender Avatar answered Dec 31 '22 13:12

Blender