Editing mixins

    Note

    Examples of how these are combined into editing views can be found at the documentation on Generic editing views.

    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_initial()

      Retrieve initial data for the form. By default, returns a copy of initial.

    • get_form_class()

      Retrieve the form class to instantiate. By default .

    • get_form(form_class=None)

      Instantiate an instance of form_class using get_form_kwargs(). If form_class isn’t provided will be used.

    • get_form_kwargs()

      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.

    • get_prefix()

      Determine the for the generated form. Returns prefix by default.

    • Determine the URL to redirect to when the form is successfully validated. Returns 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 and adds the result to the context data with the name ‘form’.

    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 the and queryset attributes, describing the type of object that the ModelForm is manipulating.

    If you specify both the and form_class attributes, an exception will be raised.

    Mixins

    Methods and Attributes

    • model

      A model class. Can be explicitly provided, otherwise will be determined by examining self.object or queryset.

    • 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 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 is provided, that class will be used. Otherwise, a ModelForm will be instantiated using the model associated with the queryset, or with the , depending on which attribute is provided.

    • get_form_kwargs()

      Add the current instance (self.object) to the standard get_form_kwargs().

    • Determine the URL to redirect to when the form is successfully validated. Returns if it is provided; otherwise, attempts to use the get_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.

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

    • post(request, \args, **kwargs*)

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

    • put(\args, **kwargs*)

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

    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 use success_url="/parent/{parent_id}/" to redirect to a URL composed out of the parent_id field on a model.

    • delete(request, \args, **kwargs*)

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