Editing mixins¶
The following mixins are used to construct Django’s editing views:
django.views.generic.edit.FormMixindjango.views.generic.edit.ModelFormMixindjango.views.generic.edit.ProcessFormViewdjango.views.generic.edit.DeletionMixin
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.
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_initial()¶ Retrieve initial data for the form. By default, returns a copy of
initial.In Django 1.3, this method was returning theinitialclass variable itself.
-
get_form_class()¶ Retrieve the form class to instantiate. By default
form_class.
-
get_form(form_class)¶ Instantiate an instance of
form_classusingget_form_kwargs().
-
get_form_kwargs()¶ Build the keyword arguments required to instantiate the form.
The
initialargument is set toget_initial(). If the request is aPOSTorPUT, the request data (request.POSTandrequest.FILES) will also be provided.
-
get_success_url()¶ Determine the URL to redirect to when the form is successfully validated. Returns
success_urlby 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)¶ Populates a context containing the contents of
kwargs.
Context
form: The form instance that was generated for the view.
Note
Views mixing
FormMixinmust provide an implementation ofform_valid()andform_invalid().-
ModelFormMixin¶
-
class
django.views.generic.edit.ModelFormMixin¶ A form mixin that works on
ModelForms, rather than a standalone form.Since this is a subclass of
SingleObjectMixin, instances of this mixin have access to themodelandquerysetattributes, describing the type of object that theModelFormis manipulating. The view also providesself.object, the instance being manipulated. If the instance is being created,self.objectwill beNone.Mixins
Methods and Attributes
-
model¶ A model class. Can be explicitly provided, otherwise will be determined by examining
self.objectorqueryset.
-
success_url¶ The URL to redirect to when the form is successfully processed.
success_urlmay contain dictionary string formatting, which will be interpolated against the object’s field attributes. For example, you could usesuccess_url="/polls/%(slug)s/"to redirect to a URL composed out of theslugfield on a model.
-
get_form_class()¶ Retrieve the form class to instantiate. If
form_classis provided, that class will be used. Otherwise, aModelFormwill 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_urlif 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()¶ 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)¶ Constructs a form, then renders a response using a context that contains that form.
-
post(request, *args, **kwargs)¶ Constructs a form, checks the form for validity, and handles it accordingly.
The PUT action is also handled, as an analog of POST.
-
-
class
django.views.generic.edit.DeletionMixin¶ Enables handling of the
DELETEhttp action.Methods and Attributes
-
success_url¶ The url to redirect to when the nominated object has been successfully deleted.
-
get_success_url()¶ Returns the url to redirect to when the nominated object has been successfully deleted. Returns
success_urlby default.
-