[Django]-Populate json to html with django template

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>
👤andyw

Leave a comment