Giter Club home page Giter Club logo

django-superform's Introduction

django-superform

Less sucking formsets.

Build Status Package Version Gitter Chat, discuss django-superform with others

Documentation | Changelog | Requirements | Installation

A SuperForm is absolutely super if you want to nest a lot of forms in each other. Use formsets and nested forms inside the SuperForm. The SuperForm will take care of its children!

Imagine you want to have a view that shows and validates a form and a formset. Let's say you have a signup form where users can enter multiple email addresses. Django provides formsets for this usecase, but handling those in a view is usually quite troublesome. You need to validate both the form and the formset manually and you cannot use django's generic FormView. So here comes django-superform into play.

Here we have an example for the usecase. Let's have a look at the forms.py:

from django import forms
from django_superform import SuperModelForm, InlineFormSetField
from myapp.models import Account, Email


class EmailForm(forms.ModelForm):
    class Meta:
        model = Email
        fields = ('account', 'email',)


EmailFormSet = modelformset_factory(EmailForm)


class SignupForm(SuperModelForm):
    username = forms.CharField()
    # The model `Email` has a ForeignKey called `user` to `Account`.
    emails = InlineFormSetField(formset_class=EmailFormSet)

    class Meta:
        model = Account
        fields = ('username',)

So we assign the EmailFormSet as a field directly to the SignupForm. That's where it belongs! Ok and how do I handle this composite form in the view? Have a look:

def post_form(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            account = form.save()
            return HttpResponseRedirect('/success/')
    else:
        form = PostForm()
    return render_to_response('post_form.html', {
        'form',
    }, context_instance=RequestContext(request))

No, we don't do anything different as we would do without having the FormSet on the SignupForm. That way you are free to implement all the logic in the form it self where it belongs and use generic views like CreateView you would use them with simple forms. Want an example for this?

from django.views.generic import CreateView
from myapp.models import Account
from myapp.forms import SignupForm


class SignupView(CreateView):
    model = Account
    form_class = SignupForm


urlpatterns = patterns('',
    url('^signup/$', SignupView.as_view()),
)

And it just works.

Requirements

  • Python 2.7 or Python 3.3+ or PyPy
  • Django 1.4+

Installation

Install the desired version with pip:

pip install django-superform

Then add django-superform to INSTALLED_APPS in your settings file:

INSTALLED_APPS = (
    # ...
    'django_superform',
    # ...
)

Development

  • Clone django-superform:

    git clone [email protected]:gregmuellegger/django-superform.git
  • cd into the repository:

    cd django-superform
  • Create a new virtualenv.
  • Install the project requirements:

    pip install -e .
    pip install -r requirements.txt
  • Run the test suite:

    tox
    # Or if you want to iterate quickly and not test against all supported
    # Python and Django versions:
    py.test

Documentation

Full documentation is available on Read the Docs: https://django-superform.readthedocs.org/

django-superform's People

Contributors

dan-passaro avatar duduklein avatar gregmuellegger avatar kiwnix avatar lucaswiman avatar mvantellingen avatar tobiase avatar

Watchers

 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.