python - Django URLs error: view must be a callable or a list/tuple in the case of include() -
after upgrading django 1.10, error:
typeerror: view must callable or list/tuple in case of include().
my urls.py follows:
urlpatterns = [ url(r'^$', 'myapp.views.home'), url(r'^contact/$', 'myapp.views.contact'), url(r'^login/$', 'django.contrib.auth.views.login'), ]
the full traceback is:
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 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 callable or list/tuple in case of include().') typeerror: view must callable or list/tuple in case of include().
django 1.10 no longer allows specify views string (e.g. 'myapp.views.home'
) in url patterns.
the solution update urls.py
include view callable. means have import view in urls.py
. if url patterns don't have names, time add one, because reversing dotted python path no longer works.
from django.contrib.auth.views import login 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 many views, importing them individually can inconvenient. alternative import views module app.
from django.contrib.auth import views auth_views myapp import views 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 have used as myapp_views
, as auth_views
, allows import views.py
multiple apps without them clashing.
see django url dispatcher docs more information urlpatterns
.
Comments
Post a Comment