Home  >  Article  >  Backend Development  >  Why am I getting a \"view must be a callable or a list/tuple in the case of include()\" error in Django URLs?

Why am I getting a \"view must be a callable or a list/tuple in the case of include()\" error in Django URLs?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 08:02:02694browse

Why am I getting a

Django URLs TypeError: "view must be a callable or a list/tuple in the case of include()."

Cause:

Starting from Django 1.10, specifying views as strings (e.g., 'myapp.views.home') in URL patterns is no longer allowed.

Solution:

Update your urls.py file to include the actual view callable:

Option 1: Import and reference views individually

<code class="python">from django.conf.urls import 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'),
]</code>

Option 2: Import the views module and reference views

<code class="python">from django.conf.urls import 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>

Note:

  • Using as allows views from multiple apps to be imported without name conflicts.
  • Consider naming URL patterns for improved readability and URL reversing.
  • Refer to the Django URL dispatcher documentation for more details.

The above is the detailed content of Why am I getting a \"view must be a callable or a list/tuple in the case of include()\" error in Django URLs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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