A CBV to handle multiple forms in one view

Overview

django-shapeshifter

A common problem in Django is how to have a view, especially a class-based view that can display and process multiple forms at once. django-shapeshifter aims to make this problem much more trivial.

Right now, django-shapeshifter can handle any (well, theoretically) number of forms in a single view. A view class is provided for multiple standard forms or model forms. To mix and match these form types, you'll need to do a little extra work. Here's how to use the package:

Installation

$ pip install django-shapeshifter

You should not need to add shapeshifter to your INSTALLED_APPS.

Usage

You use django-shapeshifter just like you use Django's built-in class-based views. You should be able to use the provided views with most mixins you're already using in your project, such as LoginRequiredMixin. Certain mixins may have to be refactored, such as SuccessMessageMixin, which is trigged on the form_valid() method.

Let's look at using the view with a few standard forms:

interests/views.py

from django.urls import reverse_lazy

from shapeshifter.views import MultiFormView

from . import forms


class InterestFormsView(MultiFormView):
    form_classes = (forms.ContactForm, forms.InterestsForm, forms.GDPRForm)
    template_name = 'interests/forms.html'
    success_url = reverse_lazy('interests:thanks')

But what do you need to do in the template? The view's context will contain a new member, forms, that you can iterate over to display each form:

interests/templates/interests/forms.html

{% extends 'layout.html' %}

{% block content %}
<h3>Please fill out your interests below!</h3>

<form method="POST">
{% csrf_token %}
{% for form in forms %}
    {{ form.as_p }}
{% endfor %}
    <input type="submit" value="Save" />
</form>
{% endblock content %}

This will generate a template with all three forms, in succession, inside of a single <form> tag. All of the forms must be submitted together. After submission, Django will fill each form in with the appropriate submitted data, validate them, and then redirect to your success_url.

But with just the above code, nothing will happen with the form data. To control that, you need to override the forms_valid method in your view. Here's what that might look like:

interests/views.py

class InterestsFormView(MultiFormView):
   ...
   def forms_valid(self):
       forms = self.get_forms()
       contact_form = forms['contactform']
       interest_form = forms['interestsform']
       gdpr = forms['gdprform']
       
       if not gdpr.data['accept']:
           messages.error("You must accept the GDPR terms.")
           return HttpResponseRedirect(reverse_lazy('interests:forms'))
       salesforce_client.send(zip(contact_form.data.items(),
                                  interest_form.data.items()))
       return super().forms_valid()

The above code isn't meant to be a complete example but should give you an idea of what would be done to handle the form data.

What about model forms?

All of the above code is valid for model forms, too, with one exception. For model forms, instead of extending MultiFormView, you'll extend MultiModelFormView. There are two major differences between the classes but the most important one is that forms_valid will call form.save() on each form. Here is an example allowing a user to edit their User first_name and last_name, and their first Profile name on one form:

my_app/models.py

from django.contrib.auth.models import User

class Profile(models.Model):
    name = models.CharField(max_length=255)
    user = models.ForeignKey(User, related_name='profiles', on_delete=models.CASCADE)

my_app/forms.py

from django.contrib.auth.models import User

from .models import Profile

class UserForm(forms.ModelForm):
    class Meta:
        model = User

        fields = [
            'first_name',
            'last_name',
        ]

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = [
            'name',
        ]

        labels = {
            'name': 'Profile Name',
        }

my_app/views.py

from shapeshifter.views import MultiModelFormView
from shapeshifter.mixins import MultiSuccessMessageMixin

from .forms import UserForm, ProfileForm

class UserUpdateView(LoginRequiredMixin, MultiSuccessMessageMixin, MultiModelFormView):
    form_classes = (UserForm, ProfileForm)
    template_name = 'my_app/forms.html'
    success_url = reverse_lazy('home')
    success_message = 'Your profile has been updated.'

    def get_instances(self):
        instances = {
            'userform': self.request.user,
            'profileform': profile_instance = Profile.objects.filter(
                user=self.request.user,
            ).first(),
        }

        return instances

What if I want to mix model and standard form?

That's fine! You will have to override forms_valid in your view to handle the processing of each form but everything else should work exactly the same.

API

MultiFormView (and MultiModelFormView by inheritance) extends Django's TemplateView. Additionally it adds a few methods for the instantiation and processing of the forms. Any and all of these can be overwritten to customize the behavior of your views.

Below is each attribute and their default value, and each method with its signature and return value.

Attributes

  • initial = {} - Initial values for each form. Should be a dict formatted with the following format:
initial = {
    'contactform': {
        'name': 'Katherine Johnson'
    }
}

where ContactForm is the class name of the form you're providing initial values for.

  • form_classes = None - a list or tuple of Form (or ModelForm if using MultiModelFormView) classes. Do not instantiate the class, just provide the name).

  • success_url = None - the URL to redirect users to once the forms are all filled in correctly. This can be a URL or a reverse_lazy instance.

Methods

  • get_form_classes(self) - Returns the view's form_classes attribute. Override this method if you need to dynamically set the forms that should be included in the view.

  • get_forms(self) -> dict - Instantiates each form, using the kwargs from get_form_kwargs and returns them all as a dict with the key being a standardized version of the form's class name. Override this if you need to change how the forms are instantiated.

  • get_form_class_name(self, form_class) -> str - Converts the form's class name into a lowercase string. ContactForm will become contactform. You can override this to provide for a different standardized name for your forms.

  • get_form_kwargs(self, form_class) -> dict - Returns a dict of keyword arguments for the form's creation. Prefixes each form with the lowercased class name, provides any initial arguments for the form, and, if the view was requested as either POST or PUT, provides both data and files to the form. For MultiModelFormView, this method also provides the instance for the form. Override this method to add or change the form kwargs.

  • validate_forms(self) -> bool - Calls form.is_valid() for each form and returns the result for the entire set of forms. Override this method if your forms require any special validation steps.

  • forms_valid(self) - This method is called if all forms pass validation. In MultiFormView, this method simply redirects to the success_url. For the model-based version, MultiModelFormView, this method calls form.save() on each form and then redirects. Override this method to change what happens when the forms are all valid.

  • forms_invalid(self) - If any of the forms fail their validation check, this method is executed. By default, it re-renders the view, presenting the forms with their errors. You can override this method if you need something else to happen when not all forms are valid.

MultiModelFormView's extra attributes and methods

As mentioned above, a few things are handled differently in MultiModelFormView.

  • instances = {} - This attribute should be a dict with lowercase form class names as keys. The values should be the instance to use for the form.

  • get_instances(self) - Returns the value of instances. Override this if you need to dynamically fetch the instances for the forms.

MultiSuccessMessageMixin attributes and methods

  • success_message = None - A string containing a success message to add through Django's messages framework.

  • get_success_message(self) - A method which returns the success message. Defaults to self.success_message.

  • forms_valid(self) - Returns the response after adding the success message.

Contributing

Thank you for your interest, time, and energy! Contributions are always welcome and will be reviewed as quickly as possible (that said, we're all volunteers with other jobs/responsibilities so it might be awhile).

Please fork this repository and make your changes in the shapeshifter package. Be sure to add a test for any functionality changes. Once all tests pass, you can submit a pull request with your changes, the rationale behind them, and any special steps the maintainers will need to take to test your changes or replicate the bug you're fixing. Be sure to include adding your name to the following list of contributors!

Contributors

  • Kenneth Love
  • Lacey Williams Henschel
  • Tim Allen

What's with the name? And the version?

The original name was already taken so a new one had to be found. Since this package deals with multiple forms, shapeshifter was a good pun (shapeshifters can take on many forms).

The version number is based on the date of release.

You might also like...
Store model history and view/revert changes from admin site.

django-simple-history django-simple-history stores Django model state on every create/update/delete. This app supports the following combinations of D

Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards and optional settings files.
Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards and optional settings files.

Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards in settings file paths and mark setti

A better and faster multiple selection widget with suggestions
A better and faster multiple selection widget with suggestions

django-searchable-select A better and faster multiple selection widget with suggestions for Django This project is looking for maintainers! Please ope

Yet another Django audit log app, hopefully the simplest one.

django-easy-audit Yet another Django audit log app, hopefully the easiest one. This app allows you to keep track of every action taken by your users.

A beginner django project and also my first Django project which involves shortening of a longer URL into a short one using a unique id.

Django-URL-Shortener A beginner django project and also my first Django project which involves shortening of a longer URL into a short one using a uni

Django query profiler - one profiler to rule them all.  Shows queries, detects N+1 and gives recommendations on how to resolve them
Django query profiler - one profiler to rule them all. Shows queries, detects N+1 and gives recommendations on how to resolve them

Django Query Profiler This is a query profiler for Django applications, for helping developers answer the question "My Django code/page/API is slow, H

A drop-in replacement for django's ImageField that provides a flexible, intuitive and easily-extensible interface for quickly creating new images from the one assigned to the field.

django-versatileimagefield A drop-in replacement for django's ImageField that provides a flexible, intuitive and easily-extensible interface for creat

A CBV to handle multiple forms in one view

django-shapeshifter A common problem in Django is how to have a view, especially a class-based view that can display and process multiple forms at onc

This app makes it extremely easy to build Django powered SPA's (Single Page App) or Mobile apps exposing all registration and authentication related functionality as CBV's (Class Base View) and REST (JSON)

Welcome to django-rest-auth Repository is unmaintained at the moment (on pause). More info can be found on this issue page: https://github.com/Tivix/d

The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML.
The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML.

django-crispy-forms The best way to have Django DRY forms. Build programmatic reusable layouts out of components, having full control of the rendered

The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML.
The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML.

django-crispy-forms The best way to have Django DRY forms. Build programmatic reusable layouts out of components, having full control of the rendered

The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML.
The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML.

django-crispy-forms The best way to have Django DRY forms. Build programmatic reusable layouts out of components, having full control of the rendered

Blender add-on: Add to Cameras menu: View → Camera, View → Add Camera, Camera → View, Previous Camera, Next Camera
Blender add-on: Add to Cameras menu: View → Camera, View → Add Camera, Camera → View, Previous Camera, Next Camera

Blender add-on: Camera additions In 3D view, it adds these actions to the View|Cameras menu: View → Camera : set the current camera to the 3D view Vie

(CVPR 2022 - oral) Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry
(CVPR 2022 - oral) Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry

Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry Official implementation of the paper Multi-View Depth Est

Endpoints is a lightweight REST api framework written in python and used in multiple production systems that handle millions of requests daily.

Endpoints Quickest API builder in the West! Endpoints is a lightweight REST api framework written in python and used in multiple production systems th

A simple chat room using socket and threading for handle multiple connections.
A simple chat room using socket and threading for handle multiple connections.

• Socket Chat Room was a little project for socket study. It works with a server handling the incoming connections from the clients. Clients send encoded messages while waiting for others clients messages simultaneously. And the server receive all the messages and delivers to the other clients.

Official PyTorch implementation of MX-Font (Multiple Heads are Better than One: Few-shot Font Generation with Multiple Localized Experts)

Introduction Pytorch implementation of Multiple Heads are Better than One: Few-shot Font Generation with Multiple Localized Expert. | paper Song Park1

One version package to rule them all, One version package to find them, One version package to bring them all, and in the darkness bind them.

AwesomeVersion One version package to rule them all, One version package to find them, One version package to bring them all, and in the darkness bind

[CVPR'21] Projecting Your View Attentively: Monocular Road Scene Layout Estimation via Cross-view Transformation
[CVPR'21] Projecting Your View Attentively: Monocular Road Scene Layout Estimation via Cross-view Transformation

Projecting Your View Attentively: Monocular Road Scene Layout Estimation via Cross-view Transformation Weixiang Yang, Qi Li, Wenxi Liu, Yuanlong Yu, Y

Comments
Releases(18.9.23)
Owner
Kenneth Love
I teach Python, I write code for my employer to get paid and open source to scratch my own itches.
Kenneth Love
Актуальный сборник шаблонов для создания проектов и приложений на Django

О чем этот проект Этот репозиторий с шаблонами для быстрого создания Django проекта. В шаблоне проекта настроены следующий технологий: Django gunicorn

Denis Kustov 16 Oct 20, 2022
A Django app for working with BTCPayServer

btcpay-django A Django app for working with BTCPayServer Installation pip install btcpay-django Developers Release To cut a release, run bumpversion,

Crawford 3 Nov 20, 2022
🗂️ 🔍 Geospatial Data Management and Search API - Django Apps

Geospatial Data API in Django Resonant GeoData (RGD) is a series of Django applications well suited for cataloging and searching annotated geospatial

Resonant GeoData 53 Nov 01, 2022
System checks for your project's environment.

django-version-checks System checks for your project's environment. Requirements Python 3.6 to 3.9 supported. Django 2.2 to 3.2 supported. Are your te

Adam Johnson 33 Dec 22, 2022
A feature flipper for Django

README Django Waffle is (yet another) feature flipper for Django. You can define the conditions for which a flag should be active, and use it in a num

950 Dec 26, 2022
Django GUID attaches a unique correlation ID/request ID to all your log outputs for every request.

Django GUID Now with ASGI support! Django GUID attaches a unique correlation ID/request ID to all your log outputs for every request. In other words,

snok 300 Dec 29, 2022
Realworld - Realworld using Django and HTMX

Realworld - Realworld using Django and HTMX

Dan Jacob 53 Jan 05, 2023
This "I P L Team Project" is developed by Prasanta Kumar Mohanty using Python with Django web framework, HTML & CSS.

I-P-L-Team-Project This "I P L Team Project" is developed by Prasanta Kumar Mohanty using Python with Django web framework, HTML & CSS. Screenshots HO

1 Dec 15, 2021
A simple trivia quizzz web app made using django

Trivia Quizzz A simple trivia quizzz web app made using django Demo http://triviaquizzz.herokuapp.com/ & https://triviaquiz.redcrypt.xyz Features Goog

Rachit Khurana 2 Feb 10, 2022
Auth module for Django and GarpixCMS

Garpix Auth Auth module for Django/DRF projects. Part of GarpixCMS. Used packages: django rest framework social-auth-app-django django-rest-framework-

GARPIX CMS 18 Mar 14, 2022
scaffold django rest apis like a champion 🚀

dr_scaffold Scaffold django rest apis like a champion ⚡ . said no one before Overview This library will help you to scaffold full Restful API Resource

Abdenasser Elidrissi 133 Jan 05, 2023
Learn Python and the Django Framework by building a e-commerce website

The Django-Ecommerce is an open-source project initiative and tutorial series built with Python and the Django Framework.

Very Academy 275 Jan 08, 2023
Domain-driven e-commerce for Django

Domain-driven e-commerce for Django Oscar is an e-commerce framework for Django designed for building domain-driven sites. It is structured such that

Oscar 5.6k Jan 01, 2023
Use webpack to generate your static bundles without django's staticfiles or opaque wrappers.

django-webpack-loader Use webpack to generate your static bundles without django's staticfiles or opaque wrappers. Django webpack loader consumes the

2.4k Dec 24, 2022
A Django app to initialize Sentry client for your Django applications

Dj_sentry This Django application intialize Sentry SDK to your Django application. How to install You can install this packaging by using: pip install

Gandi 1 Dec 09, 2021
A debug/profiling overlay for Django

Django Debug Toolbar The Django Debug Toolbar is a configurable set of panels that display various debug information about the current request/respons

David Cramer 228 Oct 17, 2022
Simple tagging for django

django-taggit This is a Jazzband project. By contributing you agree to abide by the Contributor Code of Conduct and follow the guidelines. django-tagg

Jazzband 3k Jan 02, 2023
Django-pwned - A collection of django password validators

Django Pwned A collection of django password validators. Compatibility Python: 3

Quera 22 Jun 27, 2022
Django API without Django REST framework.

Django API without DRF This is a API project made with Django, and without Django REST framework. This project was done with: Python 3.9.8 Django 3.2.

Regis Santos 3 Jan 19, 2022
A middleware to log the requests and responses using loguru.

Django Loguru The extension was based on another one and added some extra flavours. One of the biggest problems with the apps is the logging and that

Tiago Silva 9 Oct 11, 2022