升级到Django 1.10后,我收到错误:
TypeError: view must be a callable or a list/tuple in the case of include().
我的urls.py如下:
from django.conf.urls import include, url
urlpatterns = [
url(r'^$', 'myapp.views.home'),
url(r'^contact/$', 'myapp.views.contact'),
url(r'^login/$', 'django.contrib.auth.views.login'),
]
完整的追溯是:
Traceback (most recent call last):
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/alasdair/dev/urlproject/urlproject/urls.py", line 28, in <module>
url(r'^$', 'myapp.views.home'),
File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 85, in url
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
答案 0 :(得分:229)
Django 1.10不再允许您在网址格式中将视图指定为字符串(例如'myapp.views.home'
)。
解决方案是更新您的urls.py
以包含可调用的视图。这意味着您必须导入urls.py
中的视图。如果您的URL模式没有名称,那么现在是添加名称的好时机,因为使用虚线python路径进行反转不再有效。
from django.conf.urls import include, url
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'),
]
如果有很多视图,那么单独导入它们可能会很不方便。另一种方法是从您的应用程序导入视图模块。
from django.conf.urls import include, url
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'),
]
请注意,我们使用了as myapp_views
和as auth_views
,这样我们就可以从多个应用中导入views.py
,而不会发生冲突。
有关urlpatterns
。
答案 1 :(得分:3)
此错误仅表示myapp.views.home
不能像函数一样被调用。事实上它是一个字符串。虽然你的解决方案在django 1.9中运行,但是它会抛出一个警告,说这将从版本1.10开始弃用,这正是发生的事情。 @Alasdair之前的解决方案通过其中任何一个将必要的视图函数导入到脚本中
from myapp import views as myapp_views
或
from myapp.views import home, contact
答案 2 :(得分:1)
如果您有视图和模块的名称冲突,也可能会出现此错误。当我在view文件夹background-repeat:none;
下分发我的视图文件并在view2.py中导入一些名为table.py的模型时,我遇到了错误,该模型恰好是view1.py中视图的名称。因此,将视图函数命名为/views/view1.py, /views/view2.py
会有所帮助。
答案 3 :(得分:0)
您的代码是
urlpatterns = [
url(r'^$', 'myapp.views.home'),
url(r'^contact/$', 'myapp.views.contact'),
url(r'^login/$', 'django.contrib.auth.views.login'),
]
在导入include()
函数时将其更改为以下内容:
urlpatterns = [
url(r'^$', views.home),
url(r'^contact/$', views.contact),
url(r'^login/$', views.login),
]