Django URL TypeError: 在 include()的情况下,视图必须是可调用的或列表/元组

升级到 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().
118857 次浏览

Django 1.10不再允许您在 URL 模式中将视图指定为字符串(例如 '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'),
]

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.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_viewsas auth_views,它允许我们从多个应用程序导入 views.py,而不会发生冲突。

有关 urlpatterns的更多信息,请参见 Django URL 调度程序文档

这个错误只是意味着 myapp.views.home不像函数那样可以调用。它实际上是一个字符串。虽然您的解决方案可以在 django 1.9中工作,但是它仍然抛出一个警告,说这将从1.10版本开始弃用,这正是已经发生的情况。之前@Alasdair 的解决方案通过以下两种方式将必要的视图函数导入脚本 from myapp import views as myapp_views or from myapp.views import home, contact

如果视图和模块的名称发生冲突,也可能会出现此错误。在视图文件夹 /views/view1.py, /views/view2.py下分发视图文件并导入 view2.py 中名为 table.py 的模型时,出现了错误,而这恰好是 view1.py 中视图的名称。因此,将视图函数命名为 v_table(request,id) 有所帮助。

你的代码是

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),
]

change register = template.Library()registerr = template.Library() 解决了我的问题

为了防止在终端上出现错误,如果您停止服务器然后再次运行它,它可能会正常工作。 在 Windows 上:

ctrl+c

停止服务器 然后再次运行服务器:

python manage.py runserver

干杯。