Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30648 closed Cleanup/optimization (fixed)

Overriding get_context_data() is unnecessary in the "Using FormMixin with DetailView" example.

Reported by: Davit Gachechiladze Owned by: Davit Gachechiladze
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Davit Gachechiladze)

The following code snippet is from https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/#using-formmixin-with-detailview


CAUTION: you almost certainly do not want to do this.
# It is provided as part of a discussion of problems you can
# run into when combining different generic class-based view
# functionality that is not designed to be used together.

from django import forms
from django.http import HttpResponseForbidden
from django.urls import reverse
from django.views.generic import DetailView
from django.views.generic.edit import FormMixin
from books.models import Author

class AuthorInterestForm(forms.Form):
    message = forms.CharField()

class AuthorDetail(FormMixin, DetailView):
    model = Author
    form_class = AuthorInterestForm

    def get_success_url(self):
        return reverse('author-detail', kwargs={'pk': self.object.pk})

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = self.get_form()
        return context

    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return HttpResponseForbidden()
        self.object = self.get_object()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def form_valid(self, form):
        # Here, we would record the user's interest using the message
        # passed in form.cleaned_data['message']
        return super().form_valid(form)

get_success_url() is just providing somewhere to redirect to, which gets used in the default implementation of form_valid(). We have to provide our own post() as noted earlier, and override get_context_data() to make the Form available in the context data.

Sentence above is mistake, isn't it ? Because get_context_data() does not require override to capture Form inside context. It's done automatically by FormMixin.

Change History (8)

comment:1 by Davit Gachechiladze, 5 years ago

Description: modified (diff)

comment:2 by Davit Gachechiladze, 5 years ago

Type: UncategorizedBug

comment:3 by Mariusz Felisiak, 5 years ago

Easy pickings: set
Summary: Using mixins with class-based views wrong explanation of codeOverriding get_context_data() is unnecessary in the "Using FormMixin with DetailView" example.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization
Version: 2.2master

Thanks for the report. We can remove get_context_data() and , and override get_context_data() to make the Form available in the context data. from this example.

comment:4 by Davit Gachechiladze, 5 years ago

Owner: changed from nobody to Davit Gachechiladze
Status: newassigned

comment:5 by Davit Gachechiladze, 5 years ago

Has patch: set
Last edited 5 years ago by Mariusz Felisiak (previous) (diff)

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 7f612ed:

Fixed #30648 -- Removed unnecessary overriding get_context_data() from mixins with CBVs docs.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In de2635fb:

[2.2.x] Fixed #30648 -- Removed unnecessary overriding get_context_data() from mixins with CBVs docs.

Backport of 7f612eda80db1c1c8e502aced54c2062080eae46 from master

comment:8 by Davit Gachechiladze, 5 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top