Django URLs error: view must be a callable or a list/tuple in the case of include()
Posted 快乐地编程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django URLs error: view must be a callable or a list/tuple in the case of include()相关的知识,希望对你有一定的参考价值。
Django 1.10 no longer allows you to specify views as a string (e.g. ‘myapp.views.home‘
) in your URL patterns.
The solution is to update your urls.py
to include the view callable. This means that you have to import the view in your urls.py
. If your URL patterns don‘t have names, then now is a good time to add one, because reversing with the dotted python path no longer works.
from django.contrib.auth.views import login from myapp.views import home, contact urlpatterns = [ url(r‘^$‘, home, name=‘home‘), url(r‘^contact/$‘, contact, name=‘contact‘), url(r‘^login/$‘, login, name=‘login‘), ]
If there are many views, then importing them individually can be inconvenient. An alternative is to import the views module from your app.
from django.contrib.auth import views as auth_views
from myapp import views as myapp_views
urlpatterns = [
url(r‘^$‘, myapp_views.home, name=‘home‘),
url(r‘^contact/$‘, myapp_views.contact, name=‘contact‘),
url(r‘^login/$‘, auth_views.login, name=‘login‘),
]
Note that we have used as myapp_views
and as auth_views
, which allows us to import the views.py
from multiple apps without them clashing.
See the Django URL dispatcher docs for more information about urlpatterns
.
以上是关于Django URLs error: view must be a callable or a list/tuple in the case of include()的主要内容,如果未能解决你的问题,请参考以下文章
python_django_urls模块与views模块请求访问过程