ViewDoesNotExist在/

Mon*_*lal 0 python django python-3.x

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

运行此命令时:

$ 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
Run Code Online (Sandbox Code Playgroud)

这是我在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()),
]
Run Code Online (Sandbox Code Playgroud)

这是我在与urls.py相同的目录中添加到views.py中的内容:

from django.http import HttpResponse
def hello_world():
    return HttpResponse('Helloooo world!')
Run Code Online (Sandbox Code Playgroud)

你能指出我的问题原因以及解决方法吗?

在此输入图像描述

sol*_*oke 5

更换:

url(r'^$', views.hello_world()),
Run Code Online (Sandbox Code Playgroud)

有:

url(r'^$', views.hello_world),
Run Code Online (Sandbox Code Playgroud)

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

  • 要修复`TypeError`,请更改视图,使其获取请求对象:`def hello_world(request):` (3认同)