Editing mixins

فهرست فصل‌ها دانلود آرشیوناشر

Django Docs · نسخهٔ 33d2e5948a0b · BSD-3-Clause

==============

Editing mixins

The following mixins are used to construct Django's editing views:

  • django.views.generic.edit.FormMixin
  • django.views.generic.edit.ModelFormMixin
  • django.views.generic.edit.ProcessFormView
  • django.views.generic.edit.DeletionMixin

Examples of how these are combined into editing views can be found at the documentation on /ref/class-based-views/generic-editing.

`FormMixin`

A mixin class that provides facilities for creating and displaying forms.

Mixins

  • django.views.generic.base.ContextMixin

Methods and Attributes

A dictionary containing initial data for the form.

The form class to instantiate.

The URL to redirect to when the form is successfully processed.

The ~django.forms.Form.prefix for the generated form.

Retrieve initial data for the form. By default, returns a copy of ~django.views.generic.edit.FormMixin.initial.

Retrieve the form class to instantiate. By default ~django.views.generic.edit.FormMixin.form_class.

Instantiate an instance of `form_class using ~django.views.generic.edit.FormMixin.get_form_kwargs. If form_class isn't provided get_form_class` will be used.

Build the keyword arguments required to instantiate the form.

The `initial argument is set to .get_initial. If the request is a POST or PUT, the request data (request.POST and request.FILES`) will also be provided.

Determine the ~django.forms.Form.prefix for the generated form. Returns ~django.views.generic.edit.FormMixin.prefix by default.

Determine the URL to redirect to when the form is successfully validated. Returns ~django.views.generic.edit.FormMixin.success_url by default.

Redirects to ~django.views.generic.edit.FormMixin.get_success_url.

Renders a response, providing the invalid form as context.

Calls get_form and adds the result to the context data with the name 'form'.

`ModelFormMixin`

A form mixin that provides facilities for working with a `ModelForm`, rather than a standalone form.

Since this is a subclass of ~django.views.generic.detail.SingleObjectMixin, instances of this mixin have access to the ~django.views.generic.detail.SingleObjectMixin.model and ~django.views.generic.detail.SingleObjectMixin.queryset attributes, describing the type of object that the `ModelForm` is manipulating.

If you specify both the ~django.views.generic.edit.ModelFormMixin.fields and ~django.views.generic.edit.FormMixin.form_class attributes, an ~django.core.exceptions.ImproperlyConfigured exception will be raised.

Mixins

  • django.views.generic.edit.FormMixin
  • django.views.generic.detail.SingleObjectMixin

Methods and Attributes

A model class. Can be explicitly provided, otherwise will be determined by examining `self.object or ~django.views.generic.detail.SingleObjectMixin.queryset`.

A list of names of fields. This is interpreted the same way as the `Meta.fields attribute of ~django.forms.ModelForm`.

This is a required attribute if you are generating the form class automatically (e.g. using `model). Omitting this attribute will result in an ~django.core.exceptions.ImproperlyConfigured` exception.

The URL to redirect to when the form is successfully processed.

`success_url may contain dictionary string formatting, which will be interpolated against the object's field attributes. For example, you could use success_url="/polls/{slug}/" to redirect to a URL composed out of the slug` field on a model.

Retrieve the form class to instantiate. If ~django.views.generic.edit.FormMixin.form_class is provided, that class will be used. Otherwise, a `ModelForm will be instantiated using the model associated with the ~django.views.generic.detail.SingleObjectMixin.queryset, or with the ~django.views.generic.detail.SingleObjectMixin.model`, depending on which attribute is provided.

Add the current instance (`self.object) to the standard ~django.views.generic.edit.FormMixin.get_form_kwargs`.

Determine the URL to redirect to when the form is successfully validated. Returns django.views.generic.edit.ModelFormMixin.success_url if it is provided; otherwise, attempts to use the `get_absolute_url()` of the object.

Saves the form instance, sets the current object for the view, and redirects to ~django.views.generic.edit.FormMixin.get_success_url.

Renders a response, providing the invalid form as context.

`ProcessFormView`

A mixin that provides basic HTTP GET and POST workflow.

This is named 'ProcessFormView' and inherits directly from django.views.generic.base.View, but breaks if used independently, so it is more of a mixin.

Extends

  • django.views.generic.base.View

Methods and Attributes

Renders a response using a context created with ~django.views.generic.edit.FormMixin.get_context_data.

Constructs a form, checks the form for validity, and handles it accordingly.

The `PUT action is also handled and passes all parameters through to post`.

`DeletionMixin`

Enables handling of the `DELETE` HTTP action.

Methods and Attributes

The url to redirect to when the nominated object has been successfully deleted.

`success_url may contain dictionary string formatting, which will be interpolated against the object's field attributes. For example, you could use success_url="/parent/{parent_id}/" to redirect to a URL composed out of the parent_id` field on a model.

Retrieves the target object and calls its `delete()` method, then redirects to the success URL.

Returns the url to redirect to when the nominated object has been successfully deleted. Returns ~django.views.generic.edit.DeletionMixin.success_url by default.