Django REST教程DEBUG=FALSE报错
Django REST tutorial DEBUG=FALSE error
我正在尝试通过 tutorial 学习使用 django REST 框架。我已经进入 "Testing our first attempt at a Web API".
部分
当我启动服务器时,我得到:
System check identified no issues (0 silenced).
June 15, 2015 - 00:34:49
Django version 1.8, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[15/Jun/2015 00:35:17]"GET /snippets/ HTTP/1.1" 500 74404
但是当我进行下一步时:/Library/Frameworks/Python.framework/Versions/3.3/bin/http http://127.0.0.1:8000/snippets/
我收到一条错误消息,其中包括:
You're seeing this error because you have <code>DEBUG = True</code> in your
Django settings file. Change that to <code>False</code>, and Django will
display a standard page generated by the handler for this status code.
因此,当我将 settings.py 更改为:
DEBUG = False
ALLOWED_HOSTS = ['*']
然后启动服务器,出现这样的情况:
在第一个终端 window:
^$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
June 15, 2015 - 00:52:29
Django version 1.8, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
然后在第二个终端window:
$ /Library/Frameworks/Python.framework/Versions/3.3/bin/http http://127.0.0.1:8000/snippets/
HTTP/1.0 500 Internal Server Error
Content-Length: 59
Content-Type: text/plain
Date: Mon, 15 Jun 2015 00:52:42 GMT
Server: WSGIServer/0.2 CPython/3.3.5
A server error occurred. Please contact the administrator.
同时,在第一个终端 window 中,一堆结尾为:
的东西
File "/tutorial/tutorial/urls.py", line 9, in <module>
url(r'^admin/', include(snippets.urls)),
NameError: name 'snippets' is not defined
我不知道如何让它工作。我看到至少有一个人在他们自己的项目中问过一个关于 "DEBUG=False" 设置的类似问题,但坦率地说,答案超出了我的理解。有人可以在教程的上下文中解释一下吗?
非常感谢!
这个错误:
File "/tutorial/tutorial/urls.py", line 9, in <module>
url(r'^admin/', include(snippets.urls)),
NameError: name 'snippets' is not defined
出现是因为 snippets.urls
必须被引用。参见示例 here:
urlpatterns = [
url(r'^', include('snippets.urls')),
]
如图here,你可以这样写:
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
请注意第二个 include() 不包含带引号的参数。这是允许的,因为 import 语句使一个名为 admin
的变量可用,并且它有一个名为 site
的属性,而该属性又具有一个名为 urls
的属性;无论 admin.site.urls
的值是什么,它都是 include() 函数的有效参数。
但是如果你写:
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(snippets.urls))
]
然后 python 四处寻找名为 snippets 的变量,由于找不到,python 给出错误:
NameError: name 'snippets' is not defined
...
You're seeing this error because you have DEBUG = True
in
your Django settings file. Change that to False
, and
Django will display a standard page generated by the handler for
this status code.
在短语 will display a standard page generated by the handler for this status code, status code 表示 error code,这意味着你仍然会得到错误,但你不会得到错误的任何描述。当您处于生产模式(即非开发模式)时,您不希望向用户显示错误消息,因为他们不知道错误的含义,而且在任何情况下都无法更正错误。因此,在生产模式下,您设置 DEBUG=False
.
但是,当您进行开发时,您希望在出现问题时显示详细的错误消息。否则,您将看到一个网页显示:
Sorry something went wrong: 500 error. Goodbye.
这对您查找错误毫无帮助。离开 Debug=True
.
When I start the server, I get:
System check identified no issues (0 silenced).
June 15, 2015 - 00:34:49
Django version 1.8, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[15/Jun/2015 00:35:17]"GET /snippets/ HTTP/1.1" 500 74404
您不必再继续,因为那里的最后一行指示错误。对 GET 请求的响应状态代码是 500。如果您在 google 中搜索 http 状态代码 ,您将了解到 500 状态代码表示发生了错误服务器端,因此服务器无法正常响应请求,即您的代码中有错误。
我正在尝试通过 tutorial 学习使用 django REST 框架。我已经进入 "Testing our first attempt at a Web API".
部分当我启动服务器时,我得到:
System check identified no issues (0 silenced).
June 15, 2015 - 00:34:49
Django version 1.8, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[15/Jun/2015 00:35:17]"GET /snippets/ HTTP/1.1" 500 74404
但是当我进行下一步时:/Library/Frameworks/Python.framework/Versions/3.3/bin/http http://127.0.0.1:8000/snippets/
我收到一条错误消息,其中包括:
You're seeing this error because you have <code>DEBUG = True</code> in your
Django settings file. Change that to <code>False</code>, and Django will
display a standard page generated by the handler for this status code.
因此,当我将 settings.py 更改为:
DEBUG = False
ALLOWED_HOSTS = ['*']
然后启动服务器,出现这样的情况: 在第一个终端 window:
^$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
June 15, 2015 - 00:52:29
Django version 1.8, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
然后在第二个终端window:
$ /Library/Frameworks/Python.framework/Versions/3.3/bin/http http://127.0.0.1:8000/snippets/
HTTP/1.0 500 Internal Server Error
Content-Length: 59
Content-Type: text/plain
Date: Mon, 15 Jun 2015 00:52:42 GMT
Server: WSGIServer/0.2 CPython/3.3.5
A server error occurred. Please contact the administrator.
同时,在第一个终端 window 中,一堆结尾为:
的东西File "/tutorial/tutorial/urls.py", line 9, in <module>
url(r'^admin/', include(snippets.urls)),
NameError: name 'snippets' is not defined
我不知道如何让它工作。我看到至少有一个人在他们自己的项目中问过一个关于 "DEBUG=False" 设置的类似问题,但坦率地说,答案超出了我的理解。有人可以在教程的上下文中解释一下吗?
非常感谢!
这个错误:
File "/tutorial/tutorial/urls.py", line 9, in <module>
url(r'^admin/', include(snippets.urls)),
NameError: name 'snippets' is not defined
出现是因为 snippets.urls
必须被引用。参见示例 here:
urlpatterns = [
url(r'^', include('snippets.urls')),
]
如图here,你可以这样写:
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
请注意第二个 include() 不包含带引号的参数。这是允许的,因为 import 语句使一个名为 admin
的变量可用,并且它有一个名为 site
的属性,而该属性又具有一个名为 urls
的属性;无论 admin.site.urls
的值是什么,它都是 include() 函数的有效参数。
但是如果你写:
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(snippets.urls))
]
然后 python 四处寻找名为 snippets 的变量,由于找不到,python 给出错误:
NameError: name 'snippets' is not defined
...
You're seeing this error because you have
DEBUG = True
in your Django settings file. Change that toFalse
, and Django will display a standard page generated by the handler for this status code.
在短语 will display a standard page generated by the handler for this status code, status code 表示 error code,这意味着你仍然会得到错误,但你不会得到错误的任何描述。当您处于生产模式(即非开发模式)时,您不希望向用户显示错误消息,因为他们不知道错误的含义,而且在任何情况下都无法更正错误。因此,在生产模式下,您设置 DEBUG=False
.
但是,当您进行开发时,您希望在出现问题时显示详细的错误消息。否则,您将看到一个网页显示:
Sorry something went wrong: 500 error. Goodbye.
这对您查找错误毫无帮助。离开 Debug=True
.
When I start the server, I get:
System check identified no issues (0 silenced). June 15, 2015 - 00:34:49 Django version 1.8, using settings 'tutorial.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. [15/Jun/2015 00:35:17]"GET /snippets/ HTTP/1.1" 500 74404
您不必再继续,因为那里的最后一行指示错误。对 GET 请求的响应状态代码是 500。如果您在 google 中搜索 http 状态代码 ,您将了解到 500 状态代码表示发生了错误服务器端,因此服务器无法正常响应请求,即您的代码中有错误。