#29664 closed Cleanup/optimization (wontfix)
Need Proper error handling for CBV in urls.
| Reported by: | Amar | Owned by: | |
|---|---|---|---|
| Component: | Core (URLs) | Version: | 2.1 |
| Severity: | Normal | Keywords: | |
| Cc: | emiamar@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
When in urls.py if one misses to call as_view() (Example shown below) method django throws weird error. Need better way of handling error
Incorrect definition
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', HelloView)
]
Correct definition
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', HelloView.as_view())
]
Error
TypeError at /hello/ __init__() takes 1 positional argument but 2 were given Request Method: GET Request URL: http://localhost:8000/hello/ Django Version: 2.0.7 Exception Type: TypeError Exception Value: __init__() takes 1 positional argument but 2 were given Exception Location: /home/amar/.virtualenvs/djmongo/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 126
This make it really hard to figure out actual error. I faced this once and had to struggle for hours to understand what went wrong. Here is my PR
Change History (3)
comment:1 by , 7 years ago
| Description: | modified (diff) |
|---|
comment:3 by , 7 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | assigned → closed |
I have to agree 100% with Ramiro.
I’m going to close this as wontfix. If a proposal comes up that catches a user programming error here without interfering with legitimate usage then we can review but I don’t think we can consider this an Open Issue’'.
I'm afraid catching
TypeErroraround the view callable execution is a too coarse net to have. what if the view (be it either a traditional function-based view, or a correctly invoked class based view via it's.as_view()method, etc.) result in a legitimateTypeError? Wouldn't the useful traceback that cold help the developer diagnose the issue be replaced by the new one that reports"Check if as_view() method called properly"?In the end there a line that needs to be placed between handholding a developer which face this kind of problems, and the expectation of them reading the relevant documentation, i.e.
https://docs.djangoproject.com/en/dev/topics/class-based-views/#simple-usage-in-your-urlconf
https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/#using-class-based-views
I'm ok with trying to be more helpful but not at the expense of reducing the usefulness of normal Python debugging techniques.