升级到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().
Django 1.10不再允许您'myapp.views.home'
在URL模式中将视图指定为字符串(例如)。
解决方案是更新您urls.py
的视图以包含可调用的视图。这意味着您必须在中导入视图urls.py
。如果您的网址格式没有名称,那么现在是添加名称的好时机,因为使用虚线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
从多个应用程序导入,而不会发生冲突。
有关的更多信息,请参见Django
URL调度程序文档urlpatterns
。
问题内容: 我是django和python的新手。在将URL映射到视图的过程中,我遇到以下错误:TypeError:对于include(),视图必须是可调用的或列表/元组。 。py代码: views.py代码: 问题答案: 在1.10中,您不能再将导入路径传递到,而需要传递实际的view函数:
我有这个代码返回一个错误: 参数必须是实现可数的数组或对象?
我试图安装PayPal我的localhost,但当我测试payment.php得到以下警告在这个文件: 警告:sizeof():参数必须是在第176行的/Applications/MAMP/htdocs/functions/PayPal/vendor/PayPal/restapi sdk php/lib/PayPal/Common/PayPalModel.php中实现可数的数组或对象 paymen
请帮帮我 参数必须是实现可数的数组或对象 这是paypal.php代码 我没有看到错误,我已经呆了太久,寻找什么是我的错误
我在PHPmyadmin中有一些奇怪的错误。 ./libraries/pmd_common.php#405 count()中的警告:参数必须是实现可计数的数组或对象 回溯 ./db_designer.php#102:PMA_getLoadingPage(字符串'db_Name') 如何修复它?
这是我的代码片段在文件public_html/wp-内容/主题/arabv3/CustomMetaBox/CMB2.php,从第565行开始 运行代码时,出现以下错误: 参数必须是一个数组或一个对象,实现可数的