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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/054.d800fb2d00b7970667cbc7aff6463c5c%40djangoproject.com.

Reply via email to