Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Django's class-based generic views are awesome, let's have more of them.

License

NotificationsYou must be signed in to change notification settings

AndrewIngram/django-extra-views

Repository files navigation

Build StatusCoverage StatusDocumentation Status

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

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.

Installation

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',    ...]

Features

  • 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 inCreateWithInlinesView andUpdateWithInlinesView.
  • Support for naming each inline or formset in the template context withNamedFormsetsMixin.
  • 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.

Still to do

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.

Quick Examples

FormSetView

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>

ModelFormSetView

Define aModelFormSetView, a view which works asFormSetViewbut instead renders a model formset usingdjango.forms.modelformset_factory.

fromextra_viewsimportModelFormSetViewclassItemFormSetView(ModelFormSetView):model=Itemfields= ['name','sku']template_name='item_formset.html'

CreateWithInlinesView or UpdateWithInlinesView

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>

Contributing

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

Stars

Watchers

Forks

Packages

No packages published

Contributors55


[8]ページ先頭

©2009-2025 Movatter.jp