一尘不染

TypeError:对于include(),视图必须是可调用的或列表/元组

django

我是django和python的新手。在将URL映射到视图的过程中,我遇到以下错误:TypeError:对于include(),视图必须是可调用的或列表/元组。

。py代码:

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


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home 
]       
                                       # is a function in view. 

views.py代码:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
#function based views

def post_home(request):
    response = "<h1>Success</h1>"
    return HttpResponse(response)

阅读 400

收藏
2020-04-03

共1个答案

一尘不染

在1.10中,您不能再将导入路径传递到url(),而需要传递实际的view函数:

from posts.views import post_home

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