最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get rid of 'quot' in Python json.dumps - Stack Overflow

programmeradmin5浏览0评论

In Django, I have a view that requests a JSON feed and renders the response along with a template. I need to convert the JSON object to a Javascript JSON object. I have managed to console.log the JSON object in my template, but something is wrong with the format.

This is what I expect:

{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...

and this is what I get:

{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...

Here is my view:

def myView(request):
    request = Request('http://somedomain/somefeed.json')

    response = urlopen(request)
    data = json.load(response)
    return render_to_response('myTemplate.html', {'myJson': json.dumps(data)})

And in my template:

console.log('{{myJson}}');

In Django, I have a view that requests a JSON feed and renders the response along with a template. I need to convert the JSON object to a Javascript JSON object. I have managed to console.log the JSON object in my template, but something is wrong with the format.

This is what I expect:

{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...

and this is what I get:

{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...

Here is my view:

def myView(request):
    request = Request('http://somedomain/somefeed.json')

    response = urlopen(request)
    data = json.load(response)
    return render_to_response('myTemplate.html', {'myJson': json.dumps(data)})

And in my template:

console.log('{{myJson}}');
Share Improve this question edited Dec 5, 2013 at 10:10 asked Dec 5, 2013 at 10:03 user1781186user1781186 4
  • So maybe you should show the template? – Daniel Roseman Commented Dec 5, 2013 at 10:07
  • Also, why would you bother deserializing from JSON only to serialize it again immediately? What's the point? – Daniel Roseman Commented Dec 5, 2013 at 10:08
  • 4 use {{myJson|safe}} – Armance Commented Dec 5, 2013 at 10:08
  • @DanielRoseman I added the snippet from my template. – user1781186 Commented Dec 5, 2013 at 10:10
Add a ment  | 

1 Answer 1

Reset to default 19

As Armance says in the ments, you need to mark the variable as safe in the template so it is not escaped:

{{ myJson|safe }}
发布评论

评论列表(0)

  1. 暂无评论