ViewDoesNotExist在/

时间:2016-06-20 13:22:25

标签: python django python-3.x

我收到以下错误:

Asset

运行此命令时:

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

以及我在与urls.py相同的目录中添加到views.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()),
]

你能指出我的问题原因以及如何解决它吗?

enter image description here

2 个答案:

答案 0 :(得分:5)

替换:

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

使用:

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

即,不要调用该函数,只需将callable传递给url配置。

答案 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本身而不是被调用的实例。