Skip to main content

'Addons for Django 2 Formtools'

Project description

https://badge.fury.io/py/django-formtools-addons.png https://travis-ci.org/vikingco/django-formtools-addons.png?branch=master

‘Addons for Django Formtools’

Features

  • Add multiple forms to a single WizardView step (MultipleFormWizardView and subclasses)

  • Use form wizard via JSON web API (WizardAPIView)

Quickstart

Install formtools-addons:

pip install django2-formtools-addons

Then use it in a project:

# Every *MultipleForm* WizardView that can be imported is an equivalent of a builtin *WizardView in Django Formtools
from formtools_addons import (SessionMultipleFormWizardView, CookieMultipleFormWizardView,
                              NamedUrlSessionMultipleFormWizardView, NamedUrlCookieMultipleFormWizardView,
                              MultipleFormWizardView, NamedUrlMultipleFormWizardView)

# The WizardAPIView is also based on the builtin WizardView, but does not have the classic request-response cycle,
# since it exposes a JSON API
from formtools_addons import WizardAPIView

WizardAPIView: Example use

from __future__ import unicode_literals

from formtools_addons import WizardAPIView

from .forms import Form1, Form2, Form3, Form4


def show_substep_4(wizard):
    cleaned_data = wizard.get_cleaned_data_for_step('my-page1|my-substep-1') or {}
    return cleaned_data.get('some_field', None) != 'some_value'


class TestWizardAPIView(WizardAPIView):
    form_list = [
        ("my-page1", (
            ("my-substep-1", Form1),
            ("my-substep-2", Form2),
            ("my-substep-3", Form3)
        )),
        ("my-page2", (
            ('my-substep-4', Form4),
        ))
    ]

    condition_dict = {
        'my-page2|my-substep-4':   show_substep_4,
    }

    form_templates = {
        "my-page1|my-substep-1": 'demo/page1_substep1.html',
        "my-page2|my-substep-4": 'demo/page2_substep4.html',
    }

    preview_templates = {
        "my-page1|my-substep-1": 'demo/page1_substep1_preview.html',
        "my-page2|my-substep-4": 'demo/page2_substep4_preview.html',
    }

    def render_form(self, step, form):
        # Get preview template url
        template_url = self.form_templates.get(step, None)
        if template_url is None:
            data = form.cleaned_data
            return '<p>NO TEMPLATE: STEP: %s, DATA: %s</p>' % (
                step, json.dumps(data, default=self.json_encoder.default))

         # Load template
        template = get_template(template_url)

        # Create context
        context = Context()
        context['form'] = form

        return template.render(context)

    def render_preview(self, step, form):
        if not form.is_bound or not form.is_valid():
            return

        # Get preview template url
        template_url = self.preview_templates.get(step, None)
        if template_url is None:
            data = form.cleaned_data
            return '<p>NO TEMPLATE: STEP: %s, DATA: %s</p>' % (
                step, json.dumps(data, default=self.json_encoder.default))

        # Load template
        template = get_template(template_url)

        # Create context
        context = Context()
        context['data'] = form.cleaned_data if (form.is_bound and form.is_valid()) else {}

        return template.render(context)

################################################################

# testwizard/urls.py

from __future__ import unicode_literals
from django.conf.urls import url
from django.views.decorators.csrf import ensure_csrf_cookie

from .views import TestWizardAPIView

test_wizard = TestWizardAPIView.as_view(url_name='wizard')

urlpatterns = [
    # Registration Wizard API URL's
    url(r'^(?P<step>.+)/(?P<substep>.+)/$', ensure_csrf_cookie(test_wizard), name='wizard_step'),
    url(r'^(?P<step>.+)/$', ensure_csrf_cookie(test_wizard), name='wizard_step'),
]

MultipleFormWizardView: Example use

from __future__ import unicode_literals

from django import forms
from django.shortcuts import render_to_response

from formtools_addons import SessionMultipleFormWizardView

from .forms import Form1, Form2, Form3


class Wizard(SessionMultipleFormWizardView):
    form_list = [
        ("start", Form1),
        ("user_info", (
            ('account', Form2),
            ('address', Form3)
        ))
    ]

    templates = {
        "start": 'demo/wizard-start.html',
        "user_info": 'demo/wizard-user_info.html'
    }

    def get_template_names(self):
        return [self.templates[self.steps.current]]

    def done(self, form_dict, **kwargs):
        result = {}

        for key in form_dict:
            form_collection = form_dict[key]
            if isinstance(form_collection, forms.Form):
                result[key] = form_collection.cleaned_data
            elif isinstance(form_collection, dict):
                result[key] = {}
                for subkey in form_collection:
                    result[key][subkey] = form_collection[subkey].cleaned_data

        return render_to_response('demo/wizard-end.html', {
            'form_data': result,
        })

############################################################################################

form = Wizard.as_view(form_list, instance_dict={
    'start': user,  # User model instance
    'user_info': {
        'account': Account.objects.get(user=user),
        'address': Address.objects.get(user=user),
    },
})

Running Tests

$ tox

History

0.1.0 (2016-02-01)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django2-formtools-addons-0.2.2.tar.gz (35.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django2_formtools_addons-0.2.2-py2.py3-none-any.whl (47.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django2-formtools-addons-0.2.2.tar.gz.

File metadata

  • Download URL: django2-formtools-addons-0.2.2.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.5

File hashes

Hashes for django2-formtools-addons-0.2.2.tar.gz
Algorithm Hash digest
SHA256 41e1b7570d8d87cdc058b2cab8b5d2f10454b0195b3032b44a52211566e253ba
MD5 e972b8aeb00d52cc637fd30dd7b54358
BLAKE2b-256 dd0f960db93f0462096f3ed93f4a2974e595f112034e29a3c56ec95338e830fa

See more details on using hashes here.

File details

Details for the file django2_formtools_addons-0.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django2_formtools_addons-0.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 47.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.5

File hashes

Hashes for django2_formtools_addons-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 53de4734f31ca5607ee74f8b793f5e3518146c52d4144c1de61600819acccabb
MD5 4b619ced1740d9a746b129c632efd0ef
BLAKE2b-256 cba672b66acbcf97a0fba11711d198629708a3bffa75934e334d8a15777d389b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page