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.6+, Django 2.2-5.1, see tox.ini for an up-to-date list.
Full documentation is available at read the docs.
Install the stable release from pypi (using pip):
pip install django-extra-views
Or install the current master branch from github:
pip install -e git://github.com/AndrewIngram/django-extra-views.git#egg=django-extra-views
Then add 'extra_views'
to your INSTALLED_APPS
:
INSTALLED_APPS = [
...
'extra_views',
...
]
FormSet
andModelFormSet
views - The formset equivalents ofFormView
andModelFormView
.InlineFormSetView
- Lets you edit a formset related to a model (using Django'sinlineformset_factory
).CreateWithInlinesView
andUpdateWithInlinesView
- Lets you edit a model and multiple inline formsets all in one view.GenericInlineFormSetView
, the equivalent ofInlineFormSetView
but forGenericForeignKeys
.- Support for generic inlines in
CreateWithInlinesView
andUpdateWithInlinesView
. - 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
andFormSetSuccessMessageMixin
- Generic mixins to display success messages after form submission.
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.
Define a FormSetView
, a view which creates a single formset from
django.forms.formset_factory
and adds it to the context.
from extra_views import FormSetView
from my_forms import AddressForm
class AddressFormSet(FormSetView):
form_class = AddressForm
template_name = 'address_formset.html'
Then within address_formset.html
, render the formset like this:
<form method="post">
...
{{ formset }}
...
<input type="submit" value="Submit" />
</form>
Define a ModelFormSetView
, a view which works as FormSetView
but instead renders a model formset using
django.forms.modelformset_factory
.
from extra_views import ModelFormSetView
class ItemFormSetView(ModelFormSetView):
model = Item
fields = ['name', 'sku']
template_name = 'item_formset.html'
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
.
from extra_views import CreateWithInlinesView, UpdateWithInlinesView, InlineFormSetFactory
class ItemInline(InlineFormSetFactory):
model = Item
fields = ['sku', 'price', 'name']
class ContactInline(InlineFormSetFactory):
model = Contact
fields = ['name', 'email']
class CreateOrderView(CreateWithInlinesView):
model = Order
inlines = [ItemInline, ContactInline]
fields = ['customer', 'name']
template_name = 'order_and_items.html'
class UpdateOrderView(UpdateWithInlinesView):
model = Order
inlines = [ItemInline, ContactInline]
fields = ['customer', 'name']
template_name = 'order_and_items.html'
Then within order_and_items.html
, render the formset like this:
<form method="post">
...
{{ form }}
{% for formset in inlines %}
{{ formset }}
{% endfor %}
...
<input type="submit" value="Submit" />
</form>
Pull requests are welcome. To run all tests locally, setup a virtual environment and run
tox
Before committing, use pre-commit
to check all formatting and linting
pip install pre-commit
pre-commmit install
This will automatically run black
, isort
and flake8
before the commit is accepted.