Editing mixinsΒΆ
The following mixins are used to construct Djangoβs editing views:
Note
Examples of how these are combined into editing views can be found at the documentation on Generic editing views.
FormMixin
ΒΆ
- class django.views.generic.edit.FormMixinΒΆ
A mixin class that provides facilities for creating and displaying forms.
Mixins
Methods and Attributes
- initialΒΆ
A dictionary containing initial data for the form.
- form_classΒΆ
The form class to instantiate.
- success_urlΒΆ
The URL to redirect to when the form is successfully processed.
- get_form_class()ΒΆ
Retrieve the form class to instantiate. By default
form_class
.
- get_form(form_class=None)ΒΆ
Instantiate an instance of
form_class
usingget_form_kwargs()
. Ifform_class
isnβt providedget_form_class()
will be used.
- get_form_kwargs()ΒΆ
Build the keyword arguments required to instantiate the form.
The
initial
argument is set toget_initial()
. If the request is aPOST
orPUT
, the request data (request.POST
andrequest.FILES
) will also be provided.
- get_success_url()ΒΆ
Determine the URL to redirect to when the form is successfully validated. Returns
success_url
by default.
- form_valid(form)ΒΆ
Redirects to
get_success_url()
.
- form_invalid(form)ΒΆ
Renders a response, providing the invalid form as context.
- get_context_data(**kwargs)ΒΆ
Calls
get_form()
and adds the result to the context data with the name βformβ.
ModelFormMixin
ΒΆ
- class django.views.generic.edit.ModelFormMixinΒΆ
A form mixin that provides facilities for working with a
ModelForm
, rather than a standalone form.Since this is a subclass of
SingleObjectMixin
, instances of this mixin have access to themodel
andqueryset
attributes, describing the type of object that theModelForm
is manipulating.If you specify both the
fields
andform_class
attributes, anImproperlyConfigured
exception will be raised.Mixins
Methods and Attributes
- modelΒΆ
A model class. Can be explicitly provided, otherwise will be determined by examining
self.object
orqueryset
.
- fieldsΒΆ
A list of names of fields. This is interpreted the same way as the
Meta.fields
attribute ofModelForm
.This is a required attribute if you are generating the form class automatically (e.g. using
model
). Omitting this attribute will result in anImproperlyConfigured
exception.
- success_urlΒΆ
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 usesuccess_url="/polls/{slug}/"
to redirect to a URL composed out of theslug
field on a model.
- get_form_class()ΒΆ
Retrieve the form class to instantiate. If
form_class
is provided, that class will be used. Otherwise, aModelForm
will be instantiated using the model associated with thequeryset
, or with themodel
, depending on which attribute is provided.
- get_form_kwargs()ΒΆ
Add the current instance (
self.object
) to the standardget_form_kwargs()
.
- get_success_url()ΒΆ
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 theget_absolute_url()
of the object.
- form_valid(form)ΒΆ
Saves the form instance, sets the current object for the view, and redirects to
get_success_url()
.
- form_invalid(form)ΒΆ
Renders a response, providing the invalid form as context.
ProcessFormView
ΒΆ
- class django.views.generic.edit.ProcessFormViewΒΆ
A mixin that provides basic HTTP GET and POST workflow.
Note
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
Methods and Attributes
- get(request, *args, **kwargs)ΒΆ
Renders a response using a context created with
get_context_data()
.
- post(request, *args, **kwargs)ΒΆ
Constructs a form, checks the form for validity, and handles it accordingly.
DeletionMixin
ΒΆ
- class django.views.generic.edit.DeletionMixinΒΆ
Enables handling of the
DELETE
HTTP action.Methods and Attributes
- success_urlΒΆ
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 usesuccess_url="/parent/{parent_id}/"
to redirect to a URL composed out of theparent_id
field on a model.
- delete(request, *args, **kwargs)ΒΆ
Retrieves the target object and calls its
delete()
method, then redirects to the success URL.
- get_success_url()ΒΆ
Returns the url to redirect to when the nominated object has been successfully deleted. Returns
success_url
by default.