Django模板未格式化<pre> Tag Correctly

时间:2019-05-22 18:36:28

标签: html json django python-3.x django-templates

I am trying to display raw json data in a Django template with the <pre> tag.

I have this working in several places but for some reason one of my json samples is not working correctly.

To get the json I take a python dictionary with nested lists and dictionaries. I then run that through json.dumps() to get my json. It looks like this:

data = {'parent_key': {'key1': 'value1', 'key2': 'value2', 'key3': ['obj1', 'obj2', 'obj3'], 'problem_key': ['problem_data1', 'problem_data2', 'problem_data3']}

json_data = json.dumps(data, indent=4)

With my other dictionaries this works well - The expected output is 'pretty' formatted json - ie:

parent_key {
    "key1": "value1",
    "key2": "value2", 
    "key3": [
        "obj1",
        "obj2",
        "obj3",
        ], 
    "problem_key": "[\n    \"problem_data1\",\n    \"problem_data2\",\n    \"problem_data3\"\n    ]

In the Django template my code looks like so:

{% for k, v in json_data.items %}
    <pre>{{ k }} {{ v }}</pre>
{% endfor %}

As you can see most of the data formats correctly but that last section actually prints the newline characters instead of formatting them.

Any thoughts?

Edit - Render Below - search_result['display'] is a normal dic

    search_result['display'] = json.dumps(search_result['display'], indent=4)

    return render(request, 'iocs/ioc_check.html', {'search_result': search_result, 'pp_result': pp_data, 'url_haus': url_haus_,
                                                   'envcount': envcount})

Edit - Actual Output

www.google.com.dns-report.com {
    "Source": "CrowdStrike-Intel",
    "Malicious_Confidence": "high",
    "First_Seen": "2019-05-07 14:10:26",
    "Last_Seen": "2019-05-15 20:31:17",
    "TISAG_SEIR": "TISAG-SEIR",
    "Malware": [
        "FakeDead"
    ],
    "Threat_Type": "Error",
    "Kill_Chain": [
        "C2"
    ],
    "Tags": [
        "domain",
        [
            "C2"
        ],
        [
            "FakeDead"
        ]
    ],
    "Additional_Context": "[\n    {\n        \"indicator\": \"4e85c248eab9eedd3941640699cb1c4b\",\n        \"type\": \"hash_md5\",\n        \"created_date\": \"2019-05-07 14:10:26\",\n        \"last_valid_date\": \"2019-05-07 14:10:26\"\n    },\n    {\n        \"indicator\": \"96723797870a5531abec4e99fa84548837e9022e9f22074cf99973ab7df2a2e7\",\n        \"type\": \"hash_sha256\",\n        \"created_date\": \"2019-05-07 14:10:26\",\n        \"last_valid_date\": \"2019-05-07 14:10:26\"\n    },\n    {\n        \"indicator\": \"ff166de7d1b17a008e5bc3f3356fbf2dbe4906ec\",\n        \"type\": \"hash_sha1\",\n        \"created_date\": \"2019-05-07 14:10:26\",\n        \"last_valid_date\": \"2019-05-07 14:10:26\"\n    }\n]"
} 

2 个答案:

答案 0 :(得分:0)

json.dumps [Pyton-doc]的输出为str

  

(...)

     

使用此转换表将obj序列化为 JSON格式的str 。自变量与dump()中的含义相同。

如果因此要呈现结果,则可以使用以下简单的方式呈现此结果:

<pre>{{ json_data }}</pre>

由于它是str,因此也就没有.items(),因此{% for k, v in json_data.items %}应该根本没有迭代。因此,似乎您将json_data对象之外的其他内容传递给了模板。

根据您的render(..)通话,它应该类似于:

<pre>{{ search_result.display }}</pre>

经过一些讨论,看起来API响应的一部分是JSON格式的,因此在这种情况下,最好首先JSON加载该部分,然后对整个字典进行JSON转储,并像上面讨论的那样呈现它

答案 1 :(得分:0)

威廉·范·昂塞姆(Willem Van Onsem)在评论中为我找到了答案。我想发布答案,以防其他人遇到此问题。

之所以打印出奇怪的原因是因为最终结果的一小部分已经从JSON转换为字符串。为了解决这个问题,我只是将整个数据集推回到json.loads()中,然后将其重新转储回json.dumps()中,一旦完成,它就可以正常工作。