Skip to content

AndrewIngram/django-extra-views

Repository files navigation

Build Status Coverage Status Documentation Status

Django Extra Views - The missing class-based generic views for Django

Django-extra-views is a Django package which introduces additional class-based views in order to simplify common design patterns such as those found in the Django admin interface.

Supported Python and Django versions: Python 3.5+, Django 2.2-4.2, see tox.ini for an up-to-date list.

Full documentation is available at read the docs.

Installation

Install the stable release from pypi (using pip):

Or install the current master branch from github:

Then add 'extra_views' to your INSTALLED_APPS:

Features

  • FormSet and ModelFormSet views - The formset equivalents of FormView and ModelFormView.
  • InlineFormSetView - Lets you edit a formset related to a model (using Django's inlineformset_factory).
  • CreateWithInlinesView and UpdateWithInlinesView - Lets you edit a model and multiple inline formsets all in one view.
  • GenericInlineFormSetView, the equivalent of InlineFormSetView but for GenericForeignKeys.
  • Support for generic inlines in CreateWithInlinesView and UpdateWithInlinesView.
  • Support for naming each inline or formset in the template context with NamedFormsetsMixin.
  • SortableListMixin - Generic mixin for sorting functionality in your views.
  • SearchableListMixin - Generic mixin for search functionality in your views.
  • SuccessMessageMixin and FormSetSuccessMessageMixin - Generic mixins to display success messages after form submission.

Still to do

Add support for pagination in ModelFormSetView and its derivatives, the goal being to be able to mimic the change_list view in Django's admin. Currently this is proving difficult because of how Django's MultipleObjectMixin handles pagination.

Quick Examples

FormSetView

Define a FormSetView, a view which creates a single formset from django.forms.formset_factory and adds it to the context.

Then within address_formset.html, render the formset like this:

ModelFormSetView

Define a ModelFormSetView, a view which works as FormSetView but instead renders a model formset using django.forms.modelformset_factory.

CreateWithInlinesView or UpdateWithInlinesView

Define CreateWithInlinesView and UpdateWithInlinesView, views which render a form to create/update a model instance and its related inline formsets. Each of the InlineFormSetFactory classes use similar class definitions as the ModelFormSetView.

Then within order_and_items.html, render the formset like this:

Contributing

Pull requests are welcome. To run all tests locally, setup a virtual environment and run

Before committing, use pre-commit to check all formatting and linting

This will automatically run black, isort and flake8 before the commit is accepted.