Django Pretty JSON

0.4.1 · maintenance · verified Thu Apr 16

django-prettyjson is a Django library that provides a pretty JSON viewer for Django forms, admin, and templates. It's built upon jQuery JSONView and is compatible with various JSON storage methods, including raw JSON strings, Django's `JSONField` (from `django.contrib.postgres` or `django-jsonfield`), and any Python object serializable to JSON via `standardjson`. The current version is 0.4.1, and while the last code push was 9 months ago, the last official release was in 2018, indicating a maintenance release cadence.

Common errors

Warnings

Install

Imports

Quickstart

To use `django-prettyjson`, add 'prettyjson' to your `INSTALLED_APPS`. For forms and the Django admin, import `PrettyJSONWidget` and assign it to a `JSONField`'s widget. In templates, load the `prettyjson` tags, include `{% prettyjson_setup %}` in your head block (or similar), and then use `{% prettyjson your_data %}` to display JSON objects or strings beautifully.

import os
from django import forms
from django.db import models
from django.contrib import admin
from prettyjson import PrettyJSONWidget

# Add 'prettyjson' to INSTALLED_APPS in settings.py
# INSTALLED_APPS = [
#     # ...
#     'prettyjson',
# ]

# Example Model with a JSONField (requires django.contrib.postgres or django-jsonfield)
class MyData(models.Model):
    name = models.CharField(max_length=100)
    json_data = models.JSONField(default=dict)

    def __str__(self):
        return self.name

# Integrate PrettyJSONWidget in Django Admin
@admin.register(MyData)
class MyDataAdmin(admin.ModelAdmin):
    list_display = ('name',)
    formfield_overrides = {
        models.JSONField: {'widget': PrettyJSONWidget }
    }

# Or for a specific field in a custom form:
class MyDataForm(forms.ModelForm):
    class Meta:
        model = MyData
        fields = '__all__'
        widgets = {
            'json_data': PrettyJSONWidget()
        }

# For templates, include in your Django template file:
# {% load prettyjson %}
# <head>
#     {{ block.super }}
#     {% prettyjson_setup %}
# </head>
# <body>
#     {% prettyjson my_queryset %}
#     {% prettyjson my_dict %}
#     {% prettyjson '{"example": "JSON string"}' %}
# </body>

view raw JSON →