Giter Site home page Giter Site logo

theonlykingpin / django-prettyjson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kevinmickey/django-prettyjson

0.0 0.0 0.0 360 KB

Enables pretty JSON viewer in Django forms, admin, or templates

License: BSD 3-Clause "New" or "Revised" License

JavaScript 36.31% Python 43.93% CSS 5.99% Makefile 9.70% HTML 4.07%

django-prettyjson's Introduction

django-prettyjson

PyPi Version Build Status

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

Demo

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

Installation

At the command line:

pip install django-prettyjson

Configuration

Add 'prettyjson' to INSTALLED_APPS in settings.py:

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

Usage

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

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:

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 %}.

{% 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:

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

Or, in a template tag:

{% prettyjson mydict initial='parsed' %}

Running Tests

In development.

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:

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

django-prettyjson's People

Contributors

kevinmickey avatar bpartridge avatar fcurella avatar codemac avatar sdeprez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.