Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#19005 closed New feature (wontfix)

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

Reported by: tanderegg 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 3 years ago by tanderegg

  • Cc timothy.anderegg@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Version changed from 1.4 to master

comment:2 Changed 3 years ago by aaugustin

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 3 years ago by aaugustin (previous) (diff)

comment:3 Changed 3 years ago by Alex

  • Resolution set to wontfix
  • Status changed from new to closed

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 who's use should be discouraged.

Version 0, edited 3 years ago by Alex (next)

comment:4 Changed 3 years ago by tanderegg

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


comment:5 Changed 3 years ago by aaugustin

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