{"id":8107,"library":"django-prettyjson","title":"Django Pretty JSON","description":"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.","status":"maintenance","version":"0.4.1","language":"en","source_language":"en","source_url":"https://github.com/kevinmickey/django-prettyjson","tags":["django","json","admin","widget","template"],"install":[{"cmd":"pip install django-prettyjson","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The top-level package is 'prettyjson', not 'django_prettyjson'.","wrong":"from django_prettyjson import PrettyJSONWidget","symbol":"PrettyJSONWidget","correct":"from prettyjson import PrettyJSONWidget"}],"quickstart":{"code":"import os\nfrom django import forms\nfrom django.db import models\nfrom django.contrib import admin\nfrom prettyjson import PrettyJSONWidget\n\n# Add 'prettyjson' to INSTALLED_APPS in settings.py\n# INSTALLED_APPS = [\n#     # ...\n#     'prettyjson',\n# ]\n\n# Example Model with a JSONField (requires django.contrib.postgres or django-jsonfield)\nclass MyData(models.Model):\n    name = models.CharField(max_length=100)\n    json_data = models.JSONField(default=dict)\n\n    def __str__(self):\n        return self.name\n\n# Integrate PrettyJSONWidget in Django Admin\n@admin.register(MyData)\nclass MyDataAdmin(admin.ModelAdmin):\n    list_display = ('name',)\n    formfield_overrides = {\n        models.JSONField: {'widget': PrettyJSONWidget }\n    }\n\n# Or for a specific field in a custom form:\nclass MyDataForm(forms.ModelForm):\n    class Meta:\n        model = MyData\n        fields = '__all__'\n        widgets = {\n            'json_data': PrettyJSONWidget()\n        }\n\n# For templates, include in your Django template file:\n# {% load prettyjson %}\n# <head>\n#     {{ block.super }}\n#     {% prettyjson_setup %}\n# </head>\n# <body>\n#     {% prettyjson my_queryset %}\n#     {% prettyjson my_dict %}\n#     {% prettyjson '{\"example\": \"JSON string\"}' %}\n# </body>\n","lang":"python","description":"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."},"warnings":[{"fix":"Use `{% prettyjson_setup jquery=False %}` in your templates if jQuery is already present.","message":"If 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 %}`.","severity":"gotcha","affected_versions":"All"},{"fix":"Set `PrettyJSONWidget(attrs={'initial': 'parsed'})` in your form or `formfield_overrides`.","message":"By 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'}`.","severity":"gotcha","affected_versions":"All"},{"fix":"For read-only fields, consider custom admin display methods or a different approach for pretty-printing, potentially involving a custom template or JavaScript.","message":"The `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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"There 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.","severity":"gotcha","affected_versions":"0.4.1"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Add `'prettyjson'` to the `INSTALLED_APPS` tuple in your `settings.py` file.","cause":"The 'prettyjson' app was not added to your Django project's `INSTALLED_APPS` setting.","error":"ModuleNotFoundError: No module named 'prettyjson'"},{"fix":"Add `{% load prettyjson %}` at the top of your Django template file where you intend to use the tags.","cause":"You are trying to use the `prettyjson` template tags without loading them first in your template.","error":"TemplateSyntaxError: 'prettyjson' is not a registered tag library, or is not available in this version of Django."},{"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.","cause":"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.","error":"TypeError: __init__() missing 1 required positional argument: 'token' (when defining custom template tag)"}]}