Skip to main content
# django-prettyjson

[![PyPi Version](https://badge.fury.io/py/django-prettyjson.png)](https://badge.fury.io/py/django-prettyjson) [![Build Status](https://travis-ci.org/kevinmickey/django-prettyjson.svg?branch=master)](https://travis-ci.org/kevinmickey/django-prettyjson)

Enables pretty JSON viewer in Django forms, admin, or templates. The viewer is adapted from [jQuery JSONView](https://github.com/yesmeck/jquery-jsonview). It is compatible with almost anything: JSON stored in a string, a jsonfield (using django.contrib.postgres or [django-jsonfield](https://github.com/bradjasper/django-jsonfield/)), or any python object that can be serialized to JSON (using [standardjson](https://github.com/audreyr/standardjson)).

## Demo

See http://kevinmickey.github.io/django-prettyjson

## Installation

At the command line:

```sh
pip install django-prettyjson
```

## Configuration

Add `'prettyjson'` to `INSTALLED_APPS` in `settings.py`:

```python
INSTALLED_APPS = (
...,
'prettyjson',
)
```

## Usage

In a form or admin of a model, enable a pretty JSON viewer for a particular field:

```python
from prettyjson import PrettyJSONWidget

class JsonForm(forms.ModelForm):
class Meta:
model = Test
fields = '__all__'
widgets = {
'myjsonfield': PrettyJSONWidget(),
}

class JsonAdmin(admin.ModelAdmin):
form = JsonForm
```

Enable pretty JSON viewer for every JSONField of a model:

```python
from django.contrib.postgres.fields import JSONField

class JsonAdmin(admin.ModelAdmin):
formfield_overrides = {
JSONField: {'widget': PrettyJSONWidget }
}
```

In templates, you can also enable a pretty JSON viewer. Use the `prettyjson` template tag with a string JSON or with objects (dicts, QuerySets, etc.) that can be serialized to a JSON. Note that the template tag must be loaded using `{% load prettyjson %}`. It also has CSS and JS that must be included using `{% prettyjson_setup %}`.

```htmldjango
{% extends "base.html" %}

{% load prettyjson %}

{% block header %}
{{ block.super }}
{% prettyjson_setup %}
{% endblock %}

{% block content %}
{% prettyjson myqueryset %}
{% prettyjson mydict %}
{% prettyjson '{"hey": "guy","anumber": 243,"anobject": {"whoa": "nuts","anarray": [1,2,"thr<h1>ee"], "more":"stuff"},"awesome": true,"bogus": false,"meaning": null, "japanese":"明日がある。", "link": "http://jsonview.com", "notLink": "http://jsonview.com is great"}' %}
{% prettyjson '{}' %}
{% endblock %}
```

The setup includes jQuery, loaded as django.jQuery to avoid namespace conflict. If your page already includes jQuery, use `{% prettyjson_setup jquery=False %}` to avoid loading jQuery a second time.

### Configure Rendering

By default the jsonwidget will render as a raw string with a button to click to change it to parsed json. For it to render as parsed json initially, you can pass an argument:

```python
class JsonAdmin(admin.ModelAdmin):
formfield_overrides = {
JSONField: {'widget': PrettyJSONWidget(attrs={'initial': 'parsed'})}
}
```

Or, in a template tag:

```python
{% prettyjson mydict initial='parsed' %}
```

## Running Tests

In development.

```sh
source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements-test.txt
(myenv) $ python runtests.py
```

## Credits

Dependencies, parts of code, and/or sources of inspiration:

* [jQuery JSONView](https://github.com/yesmeck/jquery-jsonview)
* [standardjson](https://github.com/audreyr/standardjson)

Tools used in developing, testing, and/or rendering this package:

* [Cookiecutter](https://github.com/audreyr/cookiecutter)
* [cookiecutter-djangopackage](https://github.com/pydanny/cookiecutter-djangopackage)




History
-------

0.1.0 (2016-05-28)
++++++++++++++++++

* First release.

0.2.0 (2016-05-29)
++++++++++++++++++

* Switched to using standardjson for encoding objects
* Added setup option for those with jquery already loaded

0.2.1 (2016-07-01)
++++++++++++++++++

* Fixed manifest

0.2.2 (2016-12-07)
++++++++++++++++++

* Added dependencies to setup.py

0.3.0 (2017-05-23)
++++++++++++++++++

* Added "initial" option to show raw or parsed initial
* Requires six

0.3.1 (2018-04-01)
++++++++++++++++++

* Allows CSS control of parsed widget

0.3.2 (2018-04-09)
++++++++++++++++++

* Fixes widget media order

0.3.3 (2018-04-13)
++++++++++++++++++

* Fixes string passed as parameter

0.4.0 (2018-04-30)
++++++++++++++++++

* Allows attrs in template tag
* Fix JS for initial parsed with multiple widgets
* Change parsed CSS height to max-height

0.4.1 (2018-08-07)
++++++++++++++++++

* Fix widget for Django 2.1

Release files for django-prettyjson 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-prettyjson 0.4.1
File Size Uploaded
django-prettyjson-0.4.1.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-prettyjson 0.4.1
File Interpreter ABI Platform
django_prettyjson-0.4.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 18.8 kB

Release files / django-prettyjson-0.4.1.tar.gz

Download URL django-prettyjson-0.4.1.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b758a5f3c073db93e17485b4eb9cb19e9838f6e5d6cb91096d795e015e28d3bd
BLAKE2b-256 checksum
How to use checksums
b092fa6f4de06e85ad7b04cb26af98bac4b2d40beca0eea819e1c84f5fe145e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release files / django_prettyjson-0.4.1-py2.py3-none-any.whl

Download URL django_prettyjson-0.4.1-py2.py3-none-any.whl
Size 8.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f9f4d73899947f17a67f61b57216612373195937fa936ba9335549fe878b3355
BLAKE2b-256 checksum
How to use checksums
7cd33991e2fda0f0798d9082ba87f2cd9a2c5769fafed2e91856b97909cb00e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page