JSON serialization

101 views
Skip to first unread message

Igor Korot

unread,
Jan 18, 2014, 12:53:19 AM1/18/14
to django...@googlegroups.com
Hi, ALL,
I'd like someone to help me understand this situation.
Looking at the page: https://docs.djangoproject.com/en/dev/topics/serialization/ the part which says "Serialization formats->JSON" there is an example of how the data would be serialized.
The sample uses the symbol "double quotation".
 
On my machine I have django 1.6.1:
 
C:\Documents and Settings\Igor.FORDANWORK>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information. 
>>> import django
>>> django.get_version()
'1.6.1'
>>>
 
In my views.py I'm using following code:
 
usb_list = serializers.serialize("json", USB.objects.all())
return render_to_response('html/index.html', {"usb_data": usb_list} )
 
where USB is the data model class.
This class overrides a __unicode__() function to produce a good output on the console.
 
Now when I go to the page to see it in the browser I see the data as a JSON but the data is surrounded with a text """.
 
So in the docs I see something like this: "abc", while my application will show: "abc"
 
In the index.html I have this code:
 
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
{% load staticfiles %}
<script type="text/javascript">
    var data = "{{usb_data}}";
# some more JavaScript code
</script>
<html>
</html>
 
Is this a recent change? Am I doing something wrong?
Thank you.
 

Mario Gudelj

unread,
Jan 18, 2014, 2:32:03 AM1/18/14
to django...@googlegroups.com

In your template try {{usb_data|safe}}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/783144af-8478-408a-bdd5-0b7b5a5a1161%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Babatunde Akinyanmi

unread,
Jan 18, 2014, 3:02:01 AM1/18/14
to Django users


On 18 Jan 2014 08:32, "Mario Gudelj" <mario....@gmail.com> wrote:
>
> In your template try {{usb_data|safe}}

.......because django escapes everything in the template by default except you ask not to. One of the methods of doing so if by using the `safe` filter like Mario suggested.
You can read the documentation on how to escape or unescape stuff in your template.

> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHqTbj%3DO5ysO7cmVXFg8p-aTMbHNZ5gHA4iZ8JaVzNthVjsDkQ%40mail.gmail.com.

Igor Korot

unread,
Jan 18, 2014, 3:15:53 AM1/18/14
to django...@googlegroups.com
Hi, guys,

On Sat, Jan 18, 2014 at 12:02 AM, Babatunde Akinyanmi
<tunde...@gmail.com> wrote:
>
> On 18 Jan 2014 08:32, "Mario Gudelj" <mario....@gmail.com> wrote:
>>
>> In your template try {{usb_data|safe}}
>
> .......because django escapes everything in the template by default except
> you ask not to. One of the methods of doing so if by using the `safe` filter
> like Mario suggested.
> You can read the documentation on how to escape or unescape stuff in your
> template.

Thank you, guys. It works.
Now I need to figure out why I still don't see my data in the jQWidget
control (jqxGrid)...

Anybody familiar with this library?

Thank you.
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/uoeNHy50ozI/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2BWjgXPc0BVPpkkjOij%2BG6otewyksH7Ur7ju%3DH%3DL7%2B8HX8ZVyw%40mail.gmail.com.

Babatunde Akinyanmi

unread,
Jan 18, 2014, 11:32:30 AM1/18/14
to Django users

Thats probably because you are returning the json as a context in a response with the default "text/html" mime type. Your response should have mime type as " application/javascript".
Your json data should be in the response

Igor Korot

unread,
Jan 18, 2014, 1:48:35 PM1/18/14
to django...@googlegroups.com
Hi, Babatunde,

On Sat, Jan 18, 2014 at 8:32 AM, Babatunde Akinyanmi
<tunde...@gmail.com> wrote:
> Thats probably because you are returning the json as a context in a response
> with the default "text/html" mime type. Your response should have mime type
> as " application/javascript".
> Your json data should be in the response

How do I do that? Could you give me some code, please?
Or at least point to the docs...

Thank you.
> https://groups.google.com/d/msgid/django-users/CA%2BWjgXPS4d9tW0QW_X_cvZA7f5tn2pfcM5QdzU9gSj4nut-a2g%40mail.gmail.com.

Babatunde Akinyanmi

unread,
Jan 18, 2014, 3:05:38 PM1/18/14
to Django users

François Schiettecatte

unread,
Jan 18, 2014, 6:18:19 PM1/18/14
to django...@googlegroups.com
Hi

IANA says the JSON mime type be application/json, so you might want to use that.

application/javascript should be for javascript.


François
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BWjgXM%3Dj_jehgYYza3-2Ymf2YcYrobRHyTYnH-9q0yOVW7dGw%40mail.gmail.com.
signature.asc

Babatunde Akinyanmi

unread,
Jan 18, 2014, 11:48:46 PM1/18/14
to Django users


On 19 Jan 2014 00:18, "François Schiettecatte" <fschiet...@gmail.com> wrote:
>
> Hi
>
> IANA says the JSON mime type be application/json, so you might want to use that.
>
> application/javascript should be for javascript.

Yes thats true. My bad.

Reply all
Reply to author
Forward
0 new messages