Registry / web-framework / django-prettyjson

django-prettyjson

JSON →
library0.4.1pypypi✓ verified 91d ago

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.

pip install django-prettyjson
INSTALL
IMPORT
SIG · DJANGO-PRETTYJSON
D
django-prettyjson
web-frameworkpythonv0.4.1
Install
3.5s avg
Import
600ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.631s · 66.5MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 3.5s · import 0.569s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PrettyJSONWidget
✓ from prettyjson import PrettyJSONWidget
✗ from django_prettyjson import PrettyJSONWidget
The top-level package is 'prettyjson', not 'django_prettyjson'.

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>
Debug
Known issues
gotchaIf your page already includes jQuery, `{% prettyjson_setup %}` will load `django.jQuery` to avoid conflicts. To prevent loading jQuery a second time, use `{% prettyjson_setup jquery=False %}`.
fix
Use `{% prettyjson_setup jquery=False %}` in your templates if jQuery is already present.
affects: All
gotchaBy default, the JSON widget in forms/admin might render as a raw string with a button to parse it. To render as parsed JSON initially, you can configure the widget with `attrs={'initial': 'parsed'}`.
fix
Set `PrettyJSONWidget(attrs={'initial': 'parsed'})` in your form or `formfield_overrides`.
affects: All
gotchaThe `PrettyJSONWidget` might not apply formatting to fields explicitly marked as `readonly_fields` in the Django admin. This means the JSON content will appear as plain text.
fix
For read-only fields, consider custom admin display methods or a different approach for pretty-printing, potentially involving a custom template or JavaScript.
affects: All
gotchaThere have been reports of backslashes being added to JSON data in `jsonfield` on repeated saves when editing a model in the admin interface (Issue #17). This can lead to corrupted JSON data.
fix
Review the GitHub issue for potential workarounds or consider if this bug affects your specific Django/Python/PostgreSQL versions. Testing this behavior is recommended.
affects: 0.4.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'prettyjson'
The 'prettyjson' app was not added to your Django project's `INSTALLED_APPS` setting.
fix
Add `'prettyjson'` to the `INSTALLED_APPS` tuple in your `settings.py` file.
TemplateSyntaxError: 'prettyjson' is not a registered tag library, or is not available in this version of Django.
You are trying to use the `prettyjson` template tags without loading them first in your template.
fix
Add `{% load prettyjson %}` at the top of your Django template file where you intend to use the tags.
TypeError: __init__() missing 1 required positional argument: 'token' (when defining custom template tag)
While not directly an error from `django-prettyjson`, a common mistake when dealing with template tags (like `prettyjson`) is incorrectly defining custom ones, leading to `TemplateSyntaxError` or `TypeError` if syntax or arguments are mishandled.
fix
Ensure custom template tags adhere to Django's parsing function signature (e.g., `def do_my_tag(parser, token):`) and correctly handle `token.split_contents()` and return a `Node` instance.
Upgrade
Version history
0.4.1latest on PyPI · released Aug 7, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
django-prettyjson — pip install django-prettyjson · libregistry