Home >Backend Development >Python Tutorial >How to Fix \'TypeError: view must be a callable\' in Django 1.10?
TypeError: view must be a callable or a list/tuple in the case of include()
This error is encountered when views are specified as strings in URL patterns after upgrading to Django 1.10. Django 1.10 requires that views be specified as callables.
Solution:
To resolve this error, update your urls.py to include the view callable.
Single view import:
Import the individual view in your urls.py and specify it as a function reference:
<code class="python">from django.conf.urls import include, url from myapp.views import home, contact urlpatterns = [ url(r'^$', home, name='home'), url(r'^contact/$', contact, name='contact'), ]</code>
Multiple view import:
To avoid importing each view individually, you can import the entire views module from your app:
<code class="python">from django.conf.urls import include, url from myapp import views as myapp_views urlpatterns = [ url(r'^$', myapp_views.home, name='home'), url(r'^contact/$', myapp_views.contact, name='contact'), ]</code>
Using "as" keyword:
To prevent name clashes when importing multiple views modules from different apps, use the "as" keyword:
<code class="python">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'), ]</code>
The above is the detailed content of How to Fix \'TypeError: view must be a callable\' in Django 1.10?. For more information, please follow other related articles on the PHP Chinese website!