Django Urls Typeerror: View Must Be a Callable or a List/Tuple in the Case of Include()

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

Django 1.10 no longer allows you to specify views as a string (e.g. 'myapp.views.home') in your URL patterns.

The solution is to update your urls.py to include the view callable. This means that you have to import the view in your urls.py. If your URL patterns don't have names, then now is a good time to add one, because reversing with the dotted python path no longer works.

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'),
]

If there are many views, then importing them individually can be inconvenient. An alternative is to import the views module from your app.

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'),
]

Note that we have used as myapp_views and as auth_views, which allows us to import the views.py from multiple apps without them clashing.

See the Django URL dispatcher docs for more information about urlpatterns.

Django Newbie error: - TypeError: view must be a callable or a list/tuple in the case of include()

Just replace the url with path:

# main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.url'))
]

# app urls.py
from django.urls import path
from .view import hello

urlpatterns = [
path('hello/', hello, name='hello'),
]

As you can see in documetation, path has been introduced(from Django>=2.0), so you don't have to use url here. The tutorial you are following is for older versions, so it is using url with regex to generate URLs.

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

In 1.10, you can no longer pass import paths to url(), you need to pass the actual view function:

from posts.views import post_home

urlpatterns = [
...
url(r'^posts/$', post_home),
]

TypeError: view must be a callable or a list/tuple when including another urls.py

The error isn't from the include, it's from strings 'home' and 'people' in the urls.py that you are trying to include. Use the views that you are already importing:

from atfl.views import home, people

app_name = 'atfl'

urlpatterns = [
url(r'^$', home, name='home'),
url(r'^people/$', people, name='people'),
]

Once you've fixed that, there is a bug in your include that you should fix. The namespace is an argument to include, i.e. include('atfl.urls', namespace='atfl'). You have it as an argument to url() instead. However, in this case, you should remove the namespace from that URL pattern completely, and add app_name to the app's urls.py as above.

url(r'^atfl/', include('atfl.urls')),

Finally, don't use render_to_response. It's obsolete. Use render instead.

from django.shortcuts import render_to_response

def index(request):
return render(request, 'atfl/home.html', {})

TypeError: view must be a callable or a list/tuple in the case of include

You can import your post views in the root urls

from post import views as post_views

Then use the callable post_views.index instead of the string 'post.views.index'.

urlpatterns = [
url(r'^$', post_views.index),
url(r'^admin/', admin.site.urls)
]

Note that urlpatterns should be a list [url(...), url(...), ...]. It looks like you might have a set you have a set {url(...), url(...), ...}



Related Topics



Leave a reply



Submit