Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法获取Django模板以打印格式化的JSON_Python_Json_Django - Fatal编程技术网

Python 无法获取Django模板以打印格式化的JSON

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

我正在努力打印来自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(
            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,以便它们对渲染器可见

这里有一些有用的链接

  • ,


为什么要在将数据传递到模板之前先将数据转储到json?如果没有足够的安全性,您的for循环将可以完美地工作。转储让我离json提要更近了,但它只让我到达json提要的顶层。如何到达json提要的每一层?非常感谢Aswin。在上面的示例中,当我添加for循环时,它打印了上面例子中的“emantic_角色”和“关键字”。不是下一个级别。再次感谢。为此,您只需要嵌套For循环,就像您使用普通python代码访问它们一样。谢谢Nikhil。这很有效,上面html中的For循环确实打印json数据的顶层。现在我需要弄清楚如何与浏览html中的每个部分以打印每个级别。您不能直接对数据进行迭代。请尝试使用
数据。semantic_roles
。已接受。谢谢。Fazil,感谢您提供的附加信息和链接。非常有用。感谢您的支持。。!!