【发布时间】:2013-11-25 16:41:02
【问题描述】:
在基于类的视图之前,我的 urls.py 看起来像这样:
urlpatterns= patterns('mypackage.views',
url(r'^$', 'home', name='home'),
url(r'other', name='other'),
)
现在我的主页视图是基于类的。由于我喜欢统一性,我确实 不 想将视图类保留为字符串。我试过了:
urlpatterns= patterns('mypackage.views',
url(r'^$', 'Home.as_view', name='home'),
url(r'Other.as_view', name='other'),
)
我得到 Parent module mypackage.views.Home does not exist 。如果我简单地给出类名:
urlpatterns= patterns('mypackage.views',
url(r'^$', 'Home', name='home'),
url(r'Other', name='other'),
)
我得到:__init__ takes exactly 1 argument, 2 given
有没有办法将字符串作为第二个参数传递给 CBV 的 url 函数,而不是 from mypackage.views import * ?
编辑: 似乎没有内置的解决方案。在这种情况下:为什么字符串作为 url 函数的第二个参数允许:它不违反禅宗(“只有一种方法可以做到这一点)?
【问题讨论】:
标签: python django django-class-based-views