Python 无法获取Django模板以打印格式化的JSON
我正在努力打印来自Watson的NLU API的格式化JSON响应。我正在使用Python 2.7和Django 1.11。My views.py如下所示:Python 无法获取Django模板以打印格式化的JSON,python,json,django,Python,Json,Django,我正在努力打印来自Watson的NLU API的格式化JSON响应。我正在使用Python 2.7和Django 1.11。My views.py如下所示: def nlu_analysis(request): if request.method == 'POST': text2send = request.POST.get('text2send') natural_language_understanding = NLUV1( v
def nlu_analysis(request):
if request.method == 'POST':
text2send = request.POST.get('text2send')
natural_language_understanding = NLUV1(
version='2017-02-27',
username='####',
password='####')
response = natural_language_understanding.analyze(
text=text2send,
features=[features.Entities(), ... features.SemanticRoles()])
parsedData = json.dumps(response, indent=2)
return render(request, 'analysis.html', {'data': parsedData})
<div class="container text-left">
<p>{{ data }}</p>
</div>
My analysis.html如下所示:
def nlu_analysis(request):
if request.method == 'POST':
text2send = request.POST.get('text2send')
natural_language_understanding = NLUV1(
version='2017-02-27',
username='####',
password='####')
response = natural_language_understanding.analyze(
text=text2send,
features=[features.Entities(), ... features.SemanticRoles()])
parsedData = json.dumps(response, indent=2)
return render(request, 'analysis.html', {'data': parsedData})
<div class="container text-left">
<p>{{ data }}</p>
</div>
{{data}}
所有这些的结果就是数据,JSON括号打印在一行上,如下所示:
def nlu_analysis(request):
if request.method == 'POST':
text2send = request.POST.get('text2send')
natural_language_understanding = NLUV1(
version='2017-02-27',
username='####',
password='####')
response = natural_language_understanding.analyze(
text=text2send,
features=[features.Entities(), ... features.SemanticRoles()])
parsedData = json.dumps(response, indent=2)
return render(request, 'analysis.html', {'data': parsedData})
<div class="container text-left">
<p>{{ data }}</p>
</div>
{“语义角色”:[{“动作”:{“文本”:{“是”,“动词”:{“文本”:“是”,“时态”:“现在”},“规范化”:“是”},“句子”:“蝙蝠侠和超人正在与坏人战斗”…“关键词”:[{“相关性”:0.931284,“文本”:“坏人”},{“相关性”:0.790756,“文本”:“超人”},{“相关性”:0.752557,“文本”:“蝙蝠侠”}
如果我在for循环中运行这个
<div class="container text-left">
{% for d in data %}
<p>{{ d }}</p>
{% endfor %}
</div>
{数据%中d的%s}
{{d}
{%endfor%}
它只是在每行的字符上打印
{
“
s
e
m
暗示{{data}}是一个字符串,仅此而已
很明显,我从根本上误解了一些东西。要么是关于json.dump的方式(包括'indent=2')或者如何在我的模板中正确处理它。我怀疑是后者,因为通过“data”传递的信息显然包含所有JSON语法。如果我将上面的一行结果放在JSON验证器中,它将重新格式化并完全验证
对新手有什么帮助吗?谢谢。你为什么还要解析你的响应。
json.dumps
将dict
对象转换成字符串。你可以直接使用它作为返回渲染(请求,'analysis.html',{'data':response})
如果要访问dict对象中的键的值,只需执行以下操作(对于示例数据)
此处semantic\u角色
将被解释为文字字符串,如果模板上下文中存在变量“semantic”,则不使用该变量的值。而response
是上下文变量的名称
或者,如果要使用循环获取dict对象中每个键的值,可能需要为模板创建自定义筛选器
from django.template.defaulttags import register
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
{% for key in response %}
{{ response|get_item:key }}
{% endfor %}
在你的模板中
from django.template.defaulttags import register
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
{% for key in response %}
{{ response|get_item:key }}
{% endfor %}
当然,请确保在模板标记上调用load,以便它们对渲染器可见
这里有一些有用的链接
- ,
数据。semantic_roles
。已接受。谢谢。Fazil,感谢您提供的附加信息和链接。非常有用。感谢您的支持。。!!