简体   繁体   English

如何从 django 模板在 html 页面上打印漂亮的 JSON?

[英]How to print pretty JSON on a html page from a django template?

Why is it that in python, I can pretty print JSON with the python example below, but in a django template, it doesn't work?为什么在 python 中,我可以使用下面的 python 示例漂亮地打印 JSON,但在 django 模板中,它不起作用? How can I pretty print JSON in a django template?如何在 Django 模板中漂亮地打印 JSON?

python:蟒蛇:

import requests, json
url = 'https://api.example.com/details'
r = requests.get(url)
json_data = r.json()
json_pretty = json.dumps(json_data, sort_keys=True, indent=4)
print (json_pretty)

django views.py: Django 视图.py:

def json_list(request):
    url = 'https://api.example.com/details'
    r = requests.get(url)
    json_data = r.json()
    json_pretty = json.dumps(json_data, sort_keys=True, indent=4)

    context = {
        "json_pretty": json_pretty,
        }
    return render(request, "json_output.html", context)

template:模板:

<div>{{ json_pretty }}</div>

Why not just use the pprint filter ?为什么不直接使用pprint过滤器

view查看

context["my_json"] = {i: i for i in range(100)}

template模板

<pre>{{ my_json }}</pre>
vs    
<pre>{{ my_json | pprint }}</pre>

Screnshot截图

在此处输入图片说明

Or if you want something even better, create a custom filter或者,如果您想要更好的东西,请创建自定义过滤器

templatetags/extras.py

import json

from django import template

register = template.Library()


@register.filter
def pretty_json(value):
    return json.dumps(value, indent=4)

template模板

{% load extras %}
<pre>{{ my_json | pretty_json }}</pre>

If you just want to keep your indent, You can use如果您只想保持缩进,您可以使用

return HttpResponse(json_pretty,content_type="application/json")

If it is a must to use django template, you can use the HTML <pre> tag as suggested by Klaus.如果必须使用 django 模板,您可以使用 Klaus 建议的 HTML <pre>标签。 So your template becomes所以你的模板变成

<pre>{{ json_pretty }}</pre>

The reason it doesn't pretty up is because it renders to HTML, which handles spacing different depending on the tags you put it in. Your best bet would be to use a syntax highlighting library.它不漂亮的原因是因为它呈现为 HTML,它根据您放入的标签处理不同的间距。最好的办法是使用语法高亮库。 They are usually simple to set up.它们通常易于设置。 There are many of them, like highlight.js , prism and many, many more.其中有很多,比如highlight.jsprism等等。

I forgot to mention the Pythonic library for syntax highlighting, Pygments !我忘了提及用于语法高亮显示的 Pythonic 库Pygments

I had to use a combination of answers to get what I needed.我不得不使用答案的组合来获得我需要的东西。 The missing thing I needed was the filter linebreaks我需要的缺少的是过滤器linebreaks

So in my views.py I have a function所以在我的 views.py 我有一个功能

def foobar(request, test_id):
    json_response = json.dumps(example_dict, indent=3)
    return render(request, 'foo/bar.html', {"json_string": json_response})

and in my django template在我的 Django 模板中

<pre>{{ json_response | linebreaks }}</pre>

My json string had a bunch of \\n in it so this made it look nice by removing those (might be caused by mongoengine objects)我的 json 字符串中有一堆\\n ,所以通过删除它们看起来不错(可能是由 mongoengine 对象引起的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM