[Django] #30648: Using mixins with class-based views wrong explanation of code

9 views
Skip to first unread message

Django

unread,
Jul 18, 2019, 7:23:13 AM7/18/19
to django-...@googlegroups.com
#30648: Using mixins with class-based views wrong explanation of code
-----------------------------------------------+------------------------
Reporter: Davit Gachechiladze | Owner: nobody
Type: Uncategorized | Status: new
Component: Documentation | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------------+------------------------
The following code snippet is from 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.

{{{get_context_data()}}} does not require override to capture {{{Form}}}
inside context. It's done automatically by {{{FormMixin}}}.

--
Ticket URL: <https://code.djangoproject.com/ticket/30648>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 18, 2019, 7:24:05 AM7/18/19
to django-...@googlegroups.com
#30648: Using mixins with class-based views wrong explanation of code
-------------------------------------+-------------------------------------

Reporter: Davit Gachechiladze | Owner: nobody
Type: Uncategorized | Status: new
Component: Documentation | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

New description:

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:1>

Django

unread,
Jul 18, 2019, 7:35:48 AM7/18/19
to django-...@googlegroups.com
#30648: Using mixins with class-based views wrong explanation of code
-------------------------------------+-------------------------------------

Reporter: Davit Gachechiladze | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Davit Gachechiladze):

* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:2>

Django

unread,
Jul 18, 2019, 7:53:49 AM7/18/19
to django-...@googlegroups.com
#30648: Overriding get_context_data() is unnecessary in the "Using FormMixin with
DetailView" example.
--------------------------------------+------------------------------------

Reporter: Davit Gachechiladze | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by felixxm):

* stage: Unreviewed => Accepted
* version: 2.2 => master
* type: Bug => Cleanup/optimization
* easy: 0 => 1


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:3>

Django

unread,
Jul 18, 2019, 9:14:17 AM7/18/19
to django-...@googlegroups.com
#30648: Overriding get_context_data() is unnecessary in the "Using FormMixin with
DetailView" example.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned

Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0

-------------------------------------+-------------------------------------
Changes (by Davit Gachechiladze):

* owner: nobody => Davit Gachechiladze
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:4>

Django

unread,
Jul 18, 2019, 10:09:06 AM7/18/19
to django-...@googlegroups.com
#30648: Overriding get_context_data() is unnecessary in the "Using FormMixin with
DetailView" example.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Davit Gachechiladze):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:5>

Django

unread,
Jul 18, 2019, 1:47:46 PM7/18/19
to django-...@googlegroups.com
#30648: Overriding get_context_data() is unnecessary in the "Using FormMixin with
DetailView" example.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"7f612eda80db1c1c8e502aced54c2062080eae46" 7f612ed]:
{{{
#!CommitTicketReference repository=""
revision="7f612eda80db1c1c8e502aced54c2062080eae46"
Fixed #30648 -- Removed unnecessary overriding get_context_data() from
mixins with CBVs docs.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:6>

Django

unread,
Jul 18, 2019, 1:48:40 PM7/18/19
to django-...@googlegroups.com
#30648: Overriding get_context_data() is unnecessary in the "Using FormMixin with
DetailView" example.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"de2635fb4ea103a539d8dc080d9dee4ae0feb6d4" de2635fb]:
{{{
#!CommitTicketReference repository=""
revision="de2635fb4ea103a539d8dc080d9dee4ae0feb6d4"
[2.2.x] Fixed #30648 -- Removed unnecessary overriding get_context_data()


from mixins with CBVs docs.

Backport of 7f612eda80db1c1c8e502aced54c2062080eae46 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:7>

Django

unread,
Jul 20, 2019, 10:19:08 AM7/20/19
to django-...@googlegroups.com
#30648: Overriding get_context_data() is unnecessary in the "Using FormMixin with
DetailView" example.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0

-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

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

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

New description:

The following code snippet is from

https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins
/#using-formmixin-with-detailview

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30648#comment:8>

Reply all
Reply to author
Forward
0 new messages