Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#19005 closed New feature (wontfix)

Allow class based views to be included in by name as a string

Reported by: Tim Anderegg Owned by: nobody
Component: Core (URLs) Version: master
Severity: Normal Keywords: cbv
Cc: timothy.anderegg@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Class based views currently have to be imported into and then added to urlpatterns using ClassBasedView.as_view().

It would be relatively trivial, it seems, to add functionality to the method 'get_callable' in django.core.urlresolvers that uses inspect.isclass to determine if the lookup_view refers to a class, and if so set lookup_view = lookup_view.as_view(), throwing an exception if lookup_view is a class but the method as_view() doesn't exist.

This would help cleanup when using class based views, and be more consistent with how function based views are handled. Is there any reason not to do this? If people support this I will submit a patch.

Change History (5)

comment:1 Changed 6 years ago by Tim Anderegg

Cc: timothy.anderegg@… added
Version: 1.4master

comment:2 Changed 6 years ago by Aymeric Augustin

Importing views by name in URLconfs has a big drawback: import errors are delayed until runtime. Resulting tracebacks can be confusing. I'd prefer to move away from identifying views by name. Hence I'm -0 on this proposal.

I also have a backwards compatibility nitpick. Technically, a view can be any callable, including a class. The following class is a valid Django view:

from django.http import HttpResponse

class HelloWorld(HttpResponse):
    def __init__(self, request):
        return super(HelloWorld, self).__init__('hello world', content_type='text/plain')

I don't know if this pattern is common or even useful, but it's something that currently works, and your proposal would break it.

EDIT: after a quick poll on IRC, at least one person confesses having used this pattern, and knows other people who have too.

Last edited 6 years ago by Aymeric Augustin (previous) (diff)

comment:3 Changed 6 years ago by Alex Gaynor

Resolution: wontfix
Status: newclosed

I'm wontfixing this. It was an intentional design decision for CBVs not to have any special support in the URL system or elsewhere. Making this change would complicate the view contract, which ATM is very simple.

For bonus points I'll add that I consider the string import path thing in urls to be an anti-pattern and whose use should be discouraged.

Last edited 6 years ago by Aymeric Augustin (previous) (diff)

comment:4 Changed 6 years ago by Tim Anderegg

Fair enough, maybe I should just abandon string import paths in general then. Thanks all,


comment:5 Changed 6 years ago by Aymeric Augustin

One last note: you can use the following pattern:

class MyClassBasedView(View):
    # ...

my_class_based_view = MyClassBasedView.as_view()

and then refer to my_class_based_view by name in the URLconf. Granted, it's one line of boilerplate.

Note: See TracTickets for help on using tickets.
Back to Top