基于类的观点之前,我urls.py
看上去像是:字符串作为URL调度第二个参数为CBV
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
有没有办法将一个字符串作为第二个参数传递给url
功能CBV作为FBV而不是from mypackage.views import *
?
编辑: 似乎没有内置的解决方案。在这种情况下:为什么字符串作为url函数允许的第二个参数:是不是违反了禅宗(“只有一种方法可以做到这一点)?
这是我想弄明白,为什么有可能传递一个字符串作为第二个参数,如果它只能用于FBV? – ProfHase85
@ ProfHase85由于历史原因(即FBV)。通过导入视图对象并调用'as_view()'完成CBV。 –
FWIW,“import *”语句是恶魔。 “from mypackage.views import Home,Other” – scoopseven