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

后端 未结 7 2275
北海茫月
北海茫月 2020-12-06 10:36

I am new to django and python. During url mapping to views i am getting following error: TypeError: view must be a callable or a list/tuple in the case of include().

<
相关标签:
7条回答
  • 2020-12-06 11:22

    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),
    ]        
    
    0 讨论(0)
  • 2020-12-06 11:26

    Answer is in project-dir/urls.py

    Including another URLconf
        1. Import the include() function: from django.conf.urls import url, include
        2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
    
    0 讨论(0)
  • 2020-12-06 11:26

    Just to complement the answer from @knbk, we could use the template below:

    as is in 1.9:

    from django.conf.urls import url, include
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls), #it's not allowed to use the include() in the admin.urls
        url(r'^posts/$', include(posts.views.post_home), 
    ] 
    

    as should be in 1.10:

    from your_project_django.your_app_django.view import name_of_your_view
    
    urlpatterns = [
        ...
        url(r'^name_of_the_view/$', name_of_the_view),
    ]
    

    Remember to create in your_app_django >> views.py the function to render your view.

    0 讨论(0)
  • 2020-12-06 11:27

    For Django 1.11.2
    In the main urls.py write :

    from django.conf.urls import include,url
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^posts/', include("Post.urls")),
    ] 
    

    And in the appname/urls.py file write:

    from django.conf.urls import url
    from . import views
    
    urlpatterns = [
        url(r'^$',views.post_home),
    ]
    
    0 讨论(0)
  • 2020-12-06 11:30

    You need to pass actual view function

    from posts.views import post_home

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

    This works fine! You can have a read at URL Dispatcher Django and here Common Reguler Expressions Django URLs

    0 讨论(0)
  • 2020-12-06 11:35

    Replace your admin url pattern with this

    url(r'^admin/', include(admin.site.urls))
    

    So your urls.py becomes :

    from django.conf.urls import url, include
    from django.contrib import admin
    
    
    urlpatterns = [
        url(r'^admin/', include(admin.site.urls)),
        url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home 
    ] 
    

    admin urls are callable by include (before 1.9).

    0 讨论(0)
提交回复
热议问题