- Notifications
You must be signed in to change notification settings - Fork173
Django's class-based generic views are awesome, let's have more of them.
License
AndrewIngram/django-extra-views
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Django-extra-views is a Django package which introduces additional class-based viewsin order to simplify common design patterns such as those found in the Djangoadmin interface.
Supported Python and Django versions: Python 3.6+, Django 2.2-5.1,seetox.ini for an up-to-date list.
Full documentation is available atread 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 yourINSTALLED_APPS
:
INSTALLED_APPS= [ ...'extra_views', ...]
FormSet
andModelFormSet
views - The formset equivalents ofFormView
andModelFormView
.InlineFormSetView
- Lets you edit a formset related to a model (usingDjango'sinlineformset_factory
).CreateWithInlinesView
andUpdateWithInlinesView
- Lets you edit amodel 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 mixinsto display success messages after form submission.
Add support for pagination in ModelFormSetView and its derivatives, the goalbeing to be able to mimic the change_list view in Django's admin. Currently thisis proving difficult because of how Django's MultipleObjectMixin handles pagination.
Define aFormSetView
, a view which creates a single formset fromdjango.forms.formset_factory
and adds it to the context.
fromextra_viewsimportFormSetViewfrommy_formsimportAddressFormclassAddressFormSet(FormSetView):form_class=AddressFormtemplate_name='address_formset.html'
Then withinaddress_formset.html
, render the formset like this:
<formmethod="post"> ... {{ formset }} ...<inputtype="submit"value="Submit"/></form>
Define aModelFormSetView
, a view which works asFormSetView
but instead renders a model formset usingdjango.forms.modelformset_factory
.
fromextra_viewsimportModelFormSetViewclassItemFormSetView(ModelFormSetView):model=Itemfields= ['name','sku']template_name='item_formset.html'
DefineCreateWithInlinesView
andUpdateWithInlinesView
,views which render a form to create/update a model instance and its relatedinline formsets. Each of theInlineFormSetFactory
classes use similarclass definitions as theModelFormSetView
.
fromextra_viewsimportCreateWithInlinesView,UpdateWithInlinesView,InlineFormSetFactoryclassItemInline(InlineFormSetFactory):model=Itemfields= ['sku','price','name']classContactInline(InlineFormSetFactory):model=Contactfields= ['name','email']classCreateOrderView(CreateWithInlinesView):model=Orderinlines= [ItemInline,ContactInline]fields= ['customer','name']template_name='order_and_items.html'classUpdateOrderView(UpdateWithInlinesView):model=Orderinlines= [ItemInline,ContactInline]fields= ['customer','name']template_name='order_and_items.html'
Then withinorder_and_items.html
, render the formset like this:
<formmethod="post"> ... {{ form }} {% for formset in inlines %} {{ formset }} {% endfor %} ...<inputtype="submit"value="Submit"/></form>
Pull requests are welcome. To run all tests locally, setup a virtual environment and run
tox
Before committing, usepre-commit
to check all formatting and linting
pip install pre-commitpre-commmit install
This will automatically runblack
,isort
andflake8
before the commit is accepted.
About
Django's class-based generic views are awesome, let's have more of them.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.