How to display pretty json string in Django Template

I have the following view.

def get_queryset(self):
        """
        Excludes any questions that aren't published yet.
        """
        print('')
        print('>>>>>>>>>>>>>>>>>>> Reviews.views.DetailView.get_queryset <<<<<<<<<<<<<<<<<<<<<<<<<<<<')
        print('')
        
        return Question.objects.all()

The Question model is…

class Question(models.Model):
    jsonDefault = {'json':'sample'}

    id = models.BigAutoField(primary_key = True)
    question_text = models.CharField( null=True, max_length=200, blank=True)
    request_details = models.TextField(null=True, max_length = 250, blank=True)
    json_field = models.JSONField( null=True, blank=True,  default = dict)
    approve_and_train = models.BooleanField(null=True, default=False)
    pub_date = models.DateTimeField('date published')

The detail template is…

<h1>{{ question.question_text }}</h1>
<p>Request Details: {{question.request_details}}</p>
<p>Request JSON:{{question.json_field}}</p>
<p>Pub Date: {{question.pub_date}}</p>
<p>Approve and Train: {{question.approve_and_train}}</p>

Current output is…

Request Details: four fields

Request JSON:{'Common': {'AgentId': 'cfd6d506-cf46-4b73-8e9c-d9536390bf58', 'Payload': '/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4ACEAFddAD2IhCZDQ3hooviBOrZ/tBB4quhaqKBj8VHqr64XR+Y/7ygLjimSWeLjYpxEGJ9cX9y2h5UxYFqx0OpasVsAm4jlggCz5eJD82HKSurVIqXk/QrYrvr8AAAACj7Ufl0McgoAAXOFAQAAADXzWY+xxGf7AgAAAAAEWVo=', 'EventKey': {'Event': ', HY', 'Schema': ', HY', 'Descriptive': 'HY', 'Performance': ', HY', 'Relationship': ', HY'}, 'AuthToken': 'GlobalSaaS', 'MessageId': 'ea5ca114-eb7e-4f83-9342-646bca41a213', 'CustomerId': 'GlobalSaaS', 'DataSource': 'HealthCheck Adapter', 'DateTimeStamp': '2022-09-05T10:25:40.352231'}, 'Performance': {'UniqueKeys': '["Performance.HealthCheckSource"]', 'HealthCheckSource': 'HealthCheck Adapter', 'CustomPerformanceValues': {'Cpu': '22.1', 'Memory': '39.1'}}}

Pub Date: Nov. 7, 2022, 1:26 a.m.

Approve and Train: False

What I want is to make the JASON_Field String pretty. I tried

 and  but it didn’t like the {{question.json_field}} from the template. all I got was all the json in a single line. If i knew what the JSON string contained I could build a display, but this wont always be the same so I need something dynamic.

Can I do something in the template or expand the views.py

When you’re posting code fragments here, you need to enclose individual fragments (or an entire line) between backtick - ` characters. If you’re quoting multiple lines, it’s better to enclose them all between lines of three backticks - ```

(I’m pointing this out because your code of what you tried was “formatted out” by the forum software.)

Have you tried the pprint filter?

Thanks Ken will keep that in mind. I thought I was, but will be more careful

MO