【问题标题】:Rendering json as dictionary in template将 json 渲染为模板中的字典
【发布时间】:2011-05-16 03:42:27
【问题描述】:

我有一个从服务器返回的 JSON 对象。它看起来像这样:

{"1":{"id":"1","name":"autos"},
"2":{"id":"2","name":"business"},
"3":{"id":"3","name":"cities"},
"4":{"id":"4","name":"drama"},
"5":{"id":"5","name":"movies"},
"6":{"id":"6","name":"finance"},
"7":{"id":"7","name":"electronics"}}

所以我将模板呈现为包含我的 JSON 的字符串:

<h3>Ugly, raw list. Yuck !</h3>
1: {{ interests }}
<ul>
    {% for k,v in interests.items %}
        <li>{{k}}. - {{ v }}</li>
    {% endfor %}
</ul>

template_name = 'socialauth/interests.html'
html = render_to_string(template_name, RequestContext(request, {'interests': ResultDict,}))

结果我得到了:

<h3>Ugly, raw list. Yuck !</h3>
1: {&quot;1&quot;:{&quot;id&quot;:&quot;1&quot;,&quot;name&quot;:&quot;autos&quot;},&quot;2&quot;:{&quot;id&quot;:&quot;2&quot;,&quot;name&quot;:&quot;business&quot;},&quot;3&quot;:{&quot;id&quot;:&quot;3&quot;,&quot;name&quot;:&quot;cities&quot;},&quot;4&quot;:{&quot;id&quot;:&quot;4&quot;,&quot;name&quot;:&quot;drama&quot;},&quot;5&quot;:{&quot;id&quot;:&quot;5&quot;,&quot;name&quot;:&quot;movies&quot;},&quot;6&quot;:{&quot;id&quot;:&quot;6&quot;,&quot;name&quot;:&quot;finance&quot;},&quot;7&quot;:{&quot;id&quot;:&quot;7&quot;,&quot;name&quot;:&quot;electronics&quot;}}
<ul>   
</ul>

所以看起来我的 {{interests}} 变量没有被视为字典。但为什么 ?更重要的是,现在我将渲染列表包含到父模板中,该模板也被渲染为字符串(因为我正在使用 ajax 加载它)。最终结果如下:

模板:

<div class="connect-twitter" style="background:#f8f8f8">
    <div id="likes-list">
        {{ likes|safe }}
    </div>
    <a href="#" class="submit-step-2">Proceed</a>  
</div>

结果:

Content-Type: text/html; charset=utf-8
{"html": "<h3>Ugly, raw list. Yuck !</h3>\n\n1: {&quot;1&quot;:{&quot;id&quot;:&quot;1&quot;,&quot;name&quot;:&quot;autos&quot;},&quot;2&quot;:{&quot;id&quot;:&quot;2&quot;,&quot;name&quot;:&quot;business&quot;},&quot;3&quot;:{&quot;id&quot;:&quot;3&quot;,&quot;name&quot;:&quot;cities&quot;},&quot;4&quot;:{&quot;id&quot;:&quot;4&quot;,&quot;name&quot;:&quot;drama&quot;},&quot;5&quot;:{&quot;id&quot;:&quot;5&quot;,&quot;name&quot;:&quot;movies&quot;},&quot;6&quot;:{&quot;id&quot;:&quot;6&quot;,&quot;name&quot;:&quot;finance&quot;},&quot;7&quot;:{&quot;id&quot;:&quot;7&quot;,&quot;name&quot;:&quot;electronics&quot;}}\n\n<ul>\n    \n</ul>"}

当这段代码插入到 html 中时,它看起来很糟糕:

http://img204.imageshack.us/img204/3858/listaxv.png

什么鬼?为什么它没有正常呈现为字符串,而是添加了一些“Content-type”标头?

【问题讨论】:

  • 请显示获取 JSON 对象并将其传递给上下文的代码。

标签: python html django json dictionary


【解决方案1】:

看起来模板变量interests只是一个带有json响应的字符串。字符串在模板中被转义,这就是为什么你最终得到所有“。检查来自服务器的响应是否被正确解析。

要验证类型,可以使用类型类,即type(ResultDict)

【讨论】:

  • 首先,我的 json 被解释为字符串。所以现在我正在用 json 模块阅读它。接下来的事情是,由于我不明白的原因,request.is_ajax() 总是正确的,并且在每种情况下我都得到一个 HTTPResponse 而不仅仅是纯字符串。
【解决方案2】:

您是否对响应进行任何转换,例如 $parseJSON(string) 或 eval(string) 以将响应转换为 JS 对象?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    相关资源
    最近更新 更多