Django REST tutorial DEBUG=FALSE

2024-06-12 02:25:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过tutorial学习使用django REST框架。我已经进入了“测试我们在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 = ['*']

然后启动服务器,会发生以下情况: 在第一个终端窗口中:

^$ 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”设置的类似问题,但坦率地说,答案超出了我的想象。有人能在教程中解释一下吗?

非常感谢!


Tags: thedjangopydebugfalsehttp终端settings
1条回答
网友
1楼 · 发布于 2024-06-12 02:25:10

此错误:

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.

在短语中,将显示处理程序为此状态代码生成的标准页,状态代码表示错误代码,这意味着您仍将得到错误,但不会得到错误的任何描述。当您处于生产模式(即非开发模式)时,您不希望向不知道错误含义的用户显示错误消息,并且在任何情况下都无法更正错误。所以,在生产模式下,设置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状态码意味着服务器端出了问题,因此服务器无法正常响应请求,即您的代码中有错误。

相关问题 更多 >