Skip to main content
 首页 » 编程设计

python之Django REST教程DEBUG=FALSE报错

2024年10月24日37youxin

我正在尝试通过 tutorial 学习使用 django REST 框架.我已经进入“测试我们对 Web API 的第一次尝试”部分。

当我启动服务器时,我得到:

系统检查未发现任何问题(0 沉默)。 2015 年 6 月 15 日 - 00:34:49 Django 版本 1.8,使用设置“tutorial.settings” 在 http://127.0.0.1:8000/启动开发服务器 使用 CONTROL-C 退出服务器。 [2015 年 6 月 15 日 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 = ['*'] 

然后启动服务器,出现这样的情况: 在第一个终端窗口中:

^$ 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. 

然后在第二个终端窗口中:

$ /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. 

同时,在第一个终端窗口中,一堆以以下结尾的东西:

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 状态代码意味着服务器端出现问题,因此服务器无法正常响应请求,即您的代码中有错误。