2016-06-20 55 views
0

我收到以下錯誤:ViewDoesNotExist在/

ViewDoesNotExist at/
'<HttpResponse status_code=200, "text/html; charset=utf-8">' is not a callable or a dot-notation path 
Request Method: GET 
Request URL: http://0.0.0.0:8000/ 
Django Version: 1.9.7 
Exception Type: ViewDoesNotExist 
Exception Value:  
'<HttpResponse status_code=200, "text/html; charset=utf-8">' is not a callable or a dot-notation path 
Exception Location: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/urlresolvers.py in get_callable, line 102 
Python Executable: /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 
Python Version: 3.4.1 
Python Path:  
['/Users/mona/interviews/django/learning_site', 
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/beautifulsoup4-4.3.2-py3.4.egg', 
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', 
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', 
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', 
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', 
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages'] 
Server time: Mon, 20 Jun 2016 13:18:23 +0000 

運行此命令:

$ python3 manage.py runserver 0.0.0.0:8000 
Performing system checks... 

System check identified no issues (0 silenced). 
June 20, 2016 - 13:18:16 
Django version 1.9.7, using settings 'learning_site.settings' 
Starting development server at http://0.0.0.0:8000/ 
Quit the server with CONTROL-C. 
Internal Server Error:/
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py", line 134, in get_response 
    resolver_match = resolver.resolve(request.path_info) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/urlresolvers.py", line 376, in resolve 
    sub_match = pattern.resolve(new_path) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/urlresolvers.py", line 248, in resolve 
    return ResolverMatch(self.callback, args, kwargs, self.name) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/urlresolvers.py", line 255, in callback 
    self._callback = get_callable(self._callback_str) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/functools.py", line 428, in wrapper 
    result = user_function(*args, **kwds) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/urlresolvers.py", line 102, in get_callable 
    "'%s' is not a callable or a dot-notation path" % lookup_view 
django.core.exceptions.ViewDoesNotExist: '<HttpResponse status_code=200, "text/html; charset=utf-8">' is not a callable or a dot-notation path 
[20/Jun/2016 13:18:23] "GET/HTTP/1.1" 500 71836 

下面是我在urls.py文件:

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


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

和這裏就是我添加到與urls.py相同的目錄下的views.py中:

from django.http import HttpResponse 
def hello_world(): 
    return HttpResponse('Helloooo world!') 

你能指點我問題的原因以及如何解決它嗎?

enter image description here

+0

你的程序hello_world視圖應該採取單一位置參數:'高清程序hello_world(要求):'在/ 程序hello_world()有0位置參數,但1給出 時改爲你說 –

回答

3

在您的urlpatterns中,您提供了django應調用的函數。這意味着你只需要提供這個功能而不需要調用它們。只需在最後刪除(),它應該工作:

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

您還需要修改你的函數採取request作爲參數:

from django.http import HttpResponse 
def hello_world(request): 
    return HttpResponse('Helloooo world!') 

Django的實際上是相當不錯的,在這裏告訴你,它正在嘗試撥打HTTPRespone。這通常是告訴你應該提供callable本身,而不是被調用的實例。

5

替換:

url(r'^$', views.hello_world()), 

有:

url(r'^$', views.hello_world), 

即不調用函數,只是通過可調用的URL配置。

+0

類型錯誤'TypeError',改變視圖,以便它接受請求對象:'def hello_world(request):' –

+3

要解決得到這個錯誤 – Alasdair

相關問題