Reputation: 12173
Before class based views my urls.py
looked like that:
urlpatterns= patterns('mypackage.views',
url(r'^$', 'home', name='home'),
url(r'other', name='other'),
)
Now my home view is class based. As i like uniformity, I do not would like to keep the view class as string. I tried:
urlpatterns= patterns('mypackage.views',
url(r'^$', 'Home.as_view', name='home'),
url(r'Other.as_view', name='other'),
)
and I get Parent module mypackage.views.Home does not exist
. If I simply give the class name like:
urlpatterns= patterns('mypackage.views',
url(r'^$', 'Home', name='home'),
url(r'Other', name='other'),
)
I get: __init__ takes exactly 1 argument, 2 given
Is there a way to pass a string as second argument to the url
function for CBV as for FBV instead of from mypackage.views import *
?
EDIT: There seems to be no built-in solution for this. In this case: why are strings as second parameter allowed for the url function: Doesn't it violate the zen ("there is only one way to do this)?
Upvotes: 1
Views: 181
Reputation: 15484
If you want to pass your view as string, then in your views do this:
class Home(View):
pass
home = Home.as_view()
then in your url:
url(r'^$', 'mypackage.views.home', name='home'),
You need to use full path.
Upvotes: 2
Reputation: 122326
You should import the class-based view and specify them that way:
from mypackage.views import *
urlpatterns = patterns('mypackage.views',
url(r'^$', Home.as_view(), name='home'),
url(r'other', Other.as_view() name='other'),
)
Also note that your url()
calls should have three parameters: the URL pattern, the view and name (some examples you wrote don't have all of these).
Upvotes: 2