• Stars
    star
    108
  • Rank 320,600 (Top 7 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add forms and formsets to other forms like they were fields.

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/

More Repositories

1

django-mobile

Detect mobile browsers and serve different template flavours to them.
Python
560
star
2

django-autofixture

Can create auto-generated test data.
Python
460
star
3

django-websocket

Websocket support for django.
Python
166
star
4

django-reset

Django's reset management command for Django 1.5 and higher.
Python
21
star
5

django-debug-toolbar-autoreload

Automatically reloads your browser when a template, css or javascript file was modified.
Python
19
star
6

django-pandora

Opening Pandora's box by making django's request object available in a thread local.
Python
14
star
7

django-functional-template

Functional programming in django templates.
12
star
8

django-form-rendering-api

This repo contains ideas and samples of django's new form rendering API developed during GSoC 2011. Have a look at the README.
Python
12
star
9

django-publicmanager

The django-publicmanager application provides a custom queryset class and managers that handle the public availability of database objects. The classes provide a public method that filters by boolean is_public and date based pub_date fields.
Python
10
star
10

clickup-to-sqlite

Python
9
star
11

gsoc2011-stuff

Various files related to my GSoC 2011 project. Revised form rendering in django.
4
star
12

django-viewset

The most simplistic way to group django views
Python
4
star
13

django-ichypd

I can has your private data? - Simple app to display a form that gathers information of your users and stores it in the database. Simple use case would be the email signup form on your new startup's page that isn't online yet.
Python
2
star
14

django-app-template

My basic template for pushing out new reusable apps quickly.
Python
2
star
15

django-tinymce

Fork of django-tinymce
JavaScript
2
star
16

annotatedocs

Provides a way of adding qualityassurance annotations to Sphinx based documentations.
Python
1
star