amazingCarrotSoup amazingCarrotSoup - 2 months ago 23
Python Question

type error django include url... thing that should not be django1.11 python2.7

I am using djangos include feature in my main urls file, from my app urls file.

main urls.py

from django.conf.urls import include, url
from django.contrib import admin


urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', include('posts.urls')),
]


posts.urls.py

from django.conf.urls import url

from . import views #relative import to post views


urlpatterns = [
url(r'$',"views.posts_list" ), #list all posts
url(r'create/$',"views.posts_create" ),
url(r'detail/$',"views.posts_detail" ),
url(r'update/$',"views.posts_update" ),
url(r'delete/$',"views.posts_delete" ),
]


here is the error:

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


I have looked at the docs on this issue:
https://docs.djangoproject.com/en/1.11/ref/urls/#include

and the source code:

https://docs.djangoproject.com/en/1.11/_modules/django/conf/urls/#include

and I have no idea what I am doing wrong.

Please help. Hugs kisses, and high fives

Answer Source

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

So you should config your posts urls.py like this:

from django.conf.urls import url

from . import views


urlpatterns = [
    url(r'^$', views.posts_list, name='list'),
    url(r'^create/$', views.posts_create, name='create'),
    url(r'^detail/$', views.posts_detail, name='detail'),
    url(r'^update/$', views.posts_update, name='update'),
    url(r'^delete/$', views.posts_delete, name='delete'),
]

It is also a good practice to add name to your urls.