python - Django-rest-framework 教程获取特定片段时出现内部服务器错误

标签 python django rest django-rest-framework

遵循教程部分:http://www.django-rest-framework.org/tutorial/1-serialization/#testing-our-first-attempt-at-a-web-api

在获取特定代码段时,我收到内部服务器错误:[01/Nov/2015 15:36:47] "GET/snippets/2/HTTP/1.1"500 27:/snippets/2/ 使用 /snippets/ 获取所有片段效果很好。

该记录存在,并且我在数据中没有看到任何特别的内容。调试告诉我:

Traceback:
File "/Users/xxxx/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  151.                                  % (callback.__module__, view_name))

Exception Type: ValueError at /snippets/2/
Exception Value: The view snippets.views.snippet_detail didn't return an HttpResponse object. It returned None instead.

我在 views.py 中围绕 snippet = Snippet.objects.get(pk=pk) 放置了一些 PRINT 语句,例如:

@csrf_exempt
def snippet_detail(request, pk):
"""
Retrieve, update or delete a code snippet.
"""
print '====='
print request
print pk

try:
    print 'get snippet'
    snippet = Snippet.objects.get(pk=pk)
    print 'snippet = ' + snippet
except Snippet.DoesNotExist:
    print 'not exist'
    return HttpResponse(status=404)

这是我在终端中得到的:

=====
<WSGIRequest: GET '/snippets/2/'>
2
get snippet
[01/Nov/2015 16:06:10] "GET /snippets/2/ HTTP/1.1" 500 27

服务器错误 500。如果我尝试这样做:

>>> from snippets.models import Snippet
>>> from snippets.serializers import SnippetSerializer
>>> snippet = Snippet.objects.get(pk=2)
>>> print snippet
Snippet object
>>> print snippet.code
print "hello, world"

所以一切都应该没问题...我还可以尝试其他方法吗?

最佳答案

Exception Value: The view snippets.views.snippet_detail didn't return an HttpResponse object. It returned None instead.

你的 View 没有返回响应,当try block 执行没有异常时,它返回None,因为没有return语句(当然,除非遇到异常,否则 except 不会执行,因此您需要在 try block 中(例如,或者在整个 try/except 之后)添加 return 语句>,如教程中所示)

try:
    print 'get snippet'
    snippet = Snippet.objects.get(pk=pk)
    print 'snippet = ' + snippet
    # <--- There is no response returned here
except Snippet.DoesNotExist:
    print 'not exist'
    return HttpResponse(status=404) # <--- this response is only returned
                                    # if there is a Snippet.DoesNotExist exception

# <--- No response here neither, in case the try block executes without exception

[01/Nov/2015 16:06:10] "GET /snippets/2/ HTTP/1.1" 500 27

我发现您正在使用 GET 请求,因此就像教程中一样,您可以在 try/exception 之后使用此代码

if request.method == 'GET':
    serializer = SnippetSerializer(snippet)
    return JSONResponse(serializer.data) # <-- this is the returned response

关于python - Django-rest-framework 教程获取特定片段时出现内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33465218/

相关文章:

ruby-on-rails - rails : what is the “correct” (idiomatic) way to pass a parameter from new to create in a RESTful controller?

javascript - 使用 Express NodeJS Rest api 样板创建新路线

Mac OSX 上的 Python 路径

python - 如何从一个随机网站上抓取所有产品?

python - django ssl登录重定向到非ssl页面

rest - 以 RESTful 方式处理有序列表

Python SUDS 返回类型不是 XML

python - 为什么 (3.3==np.asarray([3.3])) 等于 [True] 而不是 False?

django - 如何在Django模板中使用域获取_absolute_url?

python - 在 django admin 中使用时与图像字段相关的问题