Skip to content

Django form's widget that renders field JSON data as group fields that can edited.

Notifications You must be signed in to change notification settings

abbasovalex/django-SplitJSONWidget-form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-split-json-widget

Build Status

A widget that renders JSON data as separate editable inputs.

Installation

pip install django-split-json-widget or pip install git+https://github.com/abbasovalex/django-SplitJSONWidget-form.git

Example №1

forms.py

# -*- coding: utf-8 -*-
from django import forms
from splitjson.widgets import SplitJSONWidget


class testForm(forms.Form):
    attrs = {'class': 'special', 'size': '40'}
    data = forms.CharField(widget=SplitJSONWidget(attrs=attrs, debug=True))

views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import testForm

def test_dict(request):
    json = {'a': 1,
            'b': 2,
            'c': 3,
            'd': 4}
    form = testForm(request.POST or None, initial={'data': json})
    if form.is_valid():
        # validate and save
        pass

    template = 'test_template.html'
    context = RequestContext(request, {'form': form})
    return render_to_response(template, context)

test_template.py

<!doctype html>
<html>
    <head></head>
	<body>
		Errors: 
        {% for field, error in form.errors.items %}
            <ul>
            <li>{{ error }}</li>
            </ul>
        {% empty %}
            no errors 
        {% endfor %}
        <hr/>
        List of:
			<form action="" method="post">
				{% csrf_token %}
				{{ form.as_p}}
				<input type="submit" value="Submit" />
			</form>
	</body>
</html>

Result (with debug mode enabled)

simple dict

Example №2

forms.py

# -*- coding: utf-8 -*-
from django import forms
from splitjson.widgets import SplitJSONWidget


class testForm(forms.Form):
    attrs = {'class': 'special', 'size': '40'}
    data = forms.CharField(widget=SplitJSONWidget(attrs=attrs, debug=True))

views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import testForm

def test_nested_dict(request):
    json = {'a': {'aa': 1},
            'b': [2, 2, {'q': 'qq',
                         'w': 'ww',
                         'z': [1, 2, 3, 4, {'five': 'number',
                                            'six': 'number'}, 7]}],
            'c': 3,
            'd': {'e':'ee', 'f':'ff'},
            'listA': [99, 98, 97, {'text': 'string'}],
            'ListA': [{'name': 'A', 'value': 'No'},
                      {'name': 'B', 'value': 'No'},
                      {'name': 'C', 'value': 'Yes'}]}
    form = testForm(request.POST or None, initial={'data': json})
    if form.is_valid():
        # validate and save
        pass

    template = 'test_template.html'
    context = RequestContext(request, {'form': form})
    return render_to_response(template, context)

test_template.py

<!doctype html>
<html>
	<head></head>
	<body>
		Errors: 
        {% for field, error in form.errors.items %}
            <ul>
            <li>{{ error }}</li>
            </ul>
        {% empty %}
            no errors 
        {% endfor %}
        <hr/>
        List of:
			<form action="" method="post">
				{% csrf_token %}
				{{ form.as_p}}
				<input type="submit" value="Submit" />
			</form>
	</body>
</html>

Result (debug mode is enabled)

test

Example №3

forms.py

# -*- coding: utf-8 -*-
from django import forms
from splitjson.widgets import SplitJSONWidget


class testForm(forms.Form):
    attrs = {'class': 'special', 'size': '40'}
    data = forms.CharField(widget=SplitJSONWidget(attrs=attrs, debug=True))

views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import testForm

def test_list(request):
    json = ['a', 'b', 'c']
    form = testForm(request.POST or None, initial={'data': json})
    if form.is_valid():
        # validate and save
        pass

    template = 'test_template.html'
    context = RequestContext(request, {'form': form})
    return render_to_response(template, context)

test_template.py

<!doctype html>
<html>
	<head></head>
	<body>
		Errors: 
        {% for field, error in form.errors.items %}
            <ul>
            <li>{{ error }}</li>
            </ul>
        {% empty %}
            no errors 
        {% endfor %}
        <hr/>
        List of:
			<form action="" method="post">
				{% csrf_token %}
				{{ form.as_p}}
				<input type="submit" value="Submit" />
			</form>
	</body>
</html>

Result (debug mode is enabled)

simple list

Example №4

forms.py

# -*- coding: utf-8 -*-
from django import forms
from splitjson.widgets import SplitJSONWidget


class testForm(forms.Form):
    attrs = {'class': 'special', 'size': '40'}
    data = forms.CharField(widget=SplitJSONWidget(attrs=attrs, debug=True))

views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import testForm

def test_nested_list(request):
    json = [['a', 'b', [1, 2, 3]], {'c': 'best'}, 'd']
    form = testForm(request.POST or None, initial={'data': json})
    if form.is_valid():
        # validate and save
        pass

    template = 'test_template.html'
    context = RequestContext(request, {'form': form})
    return render_to_response(template, context)

test_template.py

<!doctype html>
<html>
	<head></head>
	<body>
		Errors: 
        {% for field, error in form.errors.items %}
            <ul>
            <li>{{ error }}</li>
            </ul>
        {% empty %}
            no errors 
        {% endfor %}
        <hr/>
        List of:
			<form action="" method="post">
				{% csrf_token %}
				{{ form.as_p}}
				<input type="submit" value="Submit" />
			</form>
	</body>
</html>

Result (debug mode is enabled)

nested list

Known issues

See https://github.com/abbasovalex/django-SplitJSONWidget-form/issues?labels=bug&page=1&state=open

Bitdeli Badge

About

Django form's widget that renders field JSON data as group fields that can edited.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages