How to Fix \'TypeError: view must be a callable\' in Django 1.10?

Linda Hamilton
Release: 2024-10-22 08:06:17
Original
969 people have browsed it

How to Fix

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!