Opened 22 months ago

Closed 22 months ago

Last modified 22 months 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.

Attachments (0)

Change History (5)

comment:1 Changed 22 months 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 22 months 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 22 months ago by aaugustin (previous) (diff)

comment:3 Changed 22 months 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 22 months ago by Alex (next)

comment:4 Changed 22 months ago by tanderegg

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


comment:5 Changed 22 months 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.