[Django] #32195: Improve error messages from forgetting to call .as_view() on a CBV

10 views
Skip to first unread message

Django

unread,
Nov 14, 2020, 12:35:58 PM11/14/20
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
-----------------------------------------+------------------------
Reporter: Angus Holder | Owner: nobody
Type: Uncategorized | Status: new
Component: Core (URLs) | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 1 |
-----------------------------------------+------------------------
We can detect early-on that the user has forgotten to call .as_view() on
their CBV when passing it into path(). For:

{{{
urlpatterns = [
path('home', HomeView)
]
}}}

The error currently happens only when you first load the route (rather
than when constructing the routes), and looks like

{{{
Internal Server Error: /
Traceback (most recent call last):
File "C:\Users\Angus\.virtualenvs\django-WBTbdxDv\lib\site-
packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Angus\.virtualenvs\django-WBTbdxDv\lib\site-
packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
}}}

Which is especially hard to work out given that the traceback doesn't even
include any of the user's own code, and this is an error that's easy for
beginners to run into when first using CBVs.

My PR changes it to fail early, inside the call to django.urls.path(),
with a clear error:

{{{
URL route 'foo' should pass in 'EmptyCBView.as_view()' instead of
'EmptyCBView'
}}}

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

Django

unread,
Nov 14, 2020, 12:38:03 PM11/14/20
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
-------------------------------+--------------------------------------

Reporter: Angus Holder | Owner: nobody
Type: Uncategorized | Status: new
Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:

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

Old description:

> We can detect early-on that the user has forgotten to call .as_view() on
> their CBV when passing it into path(). For:
>
> {{{
> urlpatterns = [
> path('home', HomeView)
> ]
> }}}
>
> The error currently happens only when you first load the route (rather
> than when constructing the routes), and looks like
>
> {{{
> Internal Server Error: /
> Traceback (most recent call last):
> File "C:\Users\Angus\.virtualenvs\django-WBTbdxDv\lib\site-
> packages\django\core\handlers\exception.py", line 47, in inner
> response = get_response(request)
> File "C:\Users\Angus\.virtualenvs\django-WBTbdxDv\lib\site-
> packages\django\core\handlers\base.py", line 179, in _get_response
> response = wrapped_callback(request, *callback_args,
> **callback_kwargs)
> TypeError: __init__() takes 1 positional argument but 2 were given
> }}}
>
> Which is especially hard to work out given that the traceback doesn't
> even include any of the user's own code, and this is an error that's easy
> for beginners to run into when first using CBVs.
>
> My PR changes it to fail early, inside the call to django.urls.path(),
> with a clear error:
>
> {{{
> URL route 'foo' should pass in 'EmptyCBView.as_view()' instead of
> 'EmptyCBView'
> }}}

New description:

We can detect early-on that the user has forgotten to call .as_view() on
their CBV when passing it into path(). For:

{{{
urlpatterns = [
path('home', HomeView)
]
}}}

The error currently happens only when you first load the route (rather
than when constructing the routes), and looks like

{{{
Internal Server Error: /
Traceback (most recent call last):
File "C:\Users\Angus\.virtualenvs\django-WBTbdxDv\lib\site-
packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Angus\.virtualenvs\django-WBTbdxDv\lib\site-
packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
}}}

Which is especially hard to work out given that the traceback doesn't even
include any of the user's own code, and this is an error that's easy for
beginners to run into when first using CBVs.

My PR changes it to fail early, inside the call to django.urls.path(),
with a clear error:

{{{
URL route 'foo' should pass in 'EmptyCBView.as_view()' instead of
'EmptyCBView'
}}}

Pull request: https://github.com/django/django/pull/13682

--

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

Django

unread,
Nov 17, 2020, 2:49:57 AM11/17/20
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
------------------------------+------------------------------------

Reporter: Angus Holder | Owner: nobody
Type: New feature | Status: new

Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 1
------------------------------+------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted
* type: Uncategorized => New feature
* needs_docs: 0 => 1


Comment:

I agree with the motivation here. As per comments on the PR, I think we
need to leverage the System check framework for this, rather than raising
runtime errors, but other than that +1.

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

Django

unread,
Dec 8, 2020, 8:01:38 PM12/8/20
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
------------------------------+------------------------------------

Reporter: Angus Holder | Owner: nobody
Type: New feature | Status: new

Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 1
------------------------------+------------------------------------

Comment (by Craig Smith):

Would it be worthwhile to change the error message for if the `name`
keyword argument is mistakenly passed as a positional argument? Currently
the message is:


{{{
ValueError: dictionary update sequence element #0 has length 1; 2 is
required
}}}

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

Django

unread,
Jan 26, 2021, 9:49:53 AM1/26/21
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
------------------------------+------------------------------------

Reporter: Angus Holder | Owner: nobody
Type: New feature | Status: new

Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
------------------------------+------------------------------------
Changes (by Angus Holder):

* needs_better_patch: 1 => 0
* needs_docs: 1 => 0


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

Django

unread,
Mar 23, 2021, 8:01:11 AM3/23/21
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
------------------------------+----------------------------------------
Reporter: Angus Holder | Owner: Angus Holder
Type: New feature | Status: assigned

Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Angus Holder


* needs_better_patch: 0 => 1

* ui_ux: 1 => 0
* status: new => assigned


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

Django

unread,
May 28, 2021, 11:03:11 PM5/28/21
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
------------------------------+----------------------------------------
Reporter: Angus Holder | Owner: Angus Holder
Type: New feature | Status: assigned
Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0


Comment:

Author appears caught up on feedback.

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

Django

unread,
Jun 9, 2021, 2:59:44 AM6/9/21
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
-------------------------------------+-------------------------------------

Reporter: Angus Holder | Owner: Angus
| Holder
Type: New feature | Status: assigned
Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

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

* stage: Accepted => Ready for checkin


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

Django

unread,
Jun 9, 2021, 5:42:41 AM6/9/21
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
-------------------------------------+-------------------------------------
Reporter: Angus Holder | Owner: Angus
| Holder
Type: New feature | Status: closed

Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"3e73c65ffcf263d5ccd107589452a4615281a0e8" 3e73c65]:
{{{
#!CommitTicketReference repository=""
revision="3e73c65ffcf263d5ccd107589452a4615281a0e8"
Fixed #32195 -- Added system check for invalid view in path() and improved
error messages.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32195#comment:9>

Django

unread,
Jun 9, 2021, 5:42:41 AM6/9/21
to django-...@googlegroups.com
#32195: Improve error messages from forgetting to call .as_view() on a CBV
-------------------------------------+-------------------------------------
Reporter: Angus Holder | Owner: Angus
| Holder
Type: New feature | Status: assigned

Component: Core (URLs) | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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

In [changeset:"8f89454bbc873a117cc8614f2d1f1fbfd4e79ee4" 8f89454]:
{{{
#!CommitTicketReference repository=""
revision="8f89454bbc873a117cc8614f2d1f1fbfd4e79ee4"
Refs #32195 -- Added path() test for invalid view.
}}}

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

Reply all
Reply to author
Forward
0 new messages