1. 程式人生 > >Django錯誤'str' object is not callable

Django錯誤'str' object is not callable

錯誤資訊:

TypeError at /hello/
'str' object is not callable
Request Method:     GET
Request URL:    http://127.0.0.1:8000/hello/

專案結構:
這裡寫圖片描述

view.py程式碼:

from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello world ! ")

urls.py程式碼:

from django.conf.urls import patterns, url
urlpatterns = patterns(""
, url('^hello/$', "hello",name="hello"), )

原因:
沒寫應用模組名稱,找不到”hello”,
這裡寫圖片描述
解決:
修改urls.py程式碼,新增模組名:

from django.conf.urls import patterns, url
urlpatterns = patterns("puhuofile.view",
    url('^hello/$', "hello",name="hello"),
)

或者直接匯入hello,這樣就不需要模組名:

from django.conf.urls import patterns, url
from
puhuofile.view import hello urlpatterns = patterns("", url('^hello/$', hello,name="hello"), )