Coder Social home page Coder Social logo

harrywhite4 / django-jsonforms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aristotle-metadata-enterprises/django-jsonforms

0.0 2.0 0.0 147 KB

JSON Schema generated forms for Django

License: Other

Python 85.44% JavaScript 10.65% HTML 3.92%

django-jsonforms's Introduction

django-jsonforms

image

django-jsonforms provides the JSONSchemaField, a Django form field that renders a dynamic form created from a json schema. The form is created using json-editor (https://github.com/jdorn/json-editor). The field also validates submitted data against the json schema and can be rendered as a standard form field.

Installation

Install with pip pip install django-jsonforms

Add django_jsonforms to your INSTALLED_APPS

Make sure you have APP_DIRS set to True in your TEMPLATES setting More about the templates setting here: https://docs.djangoproject.com/en/2.0/ref/settings/#templates

Requirements

The python requirements jsonschema and django will be installed when installing the package through pip Additionally jQuery is required on the page that will be rendering the form.

Instructions for adding jQuery here: https://www.w3schools.com/jquery/jquery_get_started.asp

Usage

The field can be used like any other Django form field, it has the schema and options attributes which can be either python dictionaries or paths to staticfiles as as shown below

  • The schema parameter is the json schema the field will use
  • The options parameter is passed through to the json-editor object, these options are described here: https://github.com/jdorn/json-editor#options (the schema option is not used)
  • An optional ajax parameter determines whether any files are loaded via ajax on the frontend or loaded in the backend and put into html parameters (defaults to true)

Example:

from django.forms import ModelForm, Form
from django_jsonforms.forms import JSONSchemaField

class RegistrySettingsForm(Form):

    json = JSONSchemaField(
        schema = 'schema/schema.json',
        options = 'schema/options.json'
    )

In this example the schema file would be located in 'static/schema/schema.json'. You will need to run collectstatic after creating the file

Example showing all options:

class RegistrySettingsForm(Form):

    json = JSONSchemaField(
        schema = 'schema/schema.json',
        options = {"theme": "bootstrap3"},
        ajax = false
    )

Since it is common to have only one field in this type of form. A form with a single JSONSchemaField named json is also available to use

Form Example:

from django_jsonforms.forms import JSONSchemaForm

form = JSONSchemaForm(schema=... , options=... , ajax=...)

Note:

When rendering the form don't forget to render the forms media with the template tag {{ form.media }}. This is required for the field to function correctly

The data returned when the field is submitted is in the form of a python dictionary. This may need to be converted before being stored depending on the model field being used

django-jsonforms's People

Contributors

harrywhite4 avatar

Watchers

 avatar  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.