9
You just need to call json.dumps()
with the indent
keyword argument equal to the number of spaces to indent the generated JSON by for pretty printing.
For example:
json.dumps(spam_and_eggs, indent=4)
2
May I suggest using <pre></pre>
in your HTML so that your json looks pretty.
import json
from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import render
from .models import Scan
@staff_member_required
def info(request):
my_info = Scan.scan()
return render(request, 'info.html', {'info': json.dumps(my_info, indent=4)})
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<pre>{{ info|safe }}</pre>
</body>
</html>
- [Django]-Django ImportError at / no matter what I do
- [Django]-How install a extension in postgresql before creating the models in django?
- [Django]-Django Nonrel – Working around multi-table inheritance with noSQL?
Source:stackexchange.com