【发布时间】:2019-09-18 06:26:17
【问题描述】:
我正在尝试使用 Django Rest 框架构建一个 Web 应用程序。
当我在 localhost 127.0.0.0/8000 上运行应用程序时,它运行良好。但是当我在 EC2 服务器上部署它时,POST 方法返回 500 服务器错误。
这里是views.py中的post方法之一——
class customer_location(APIView):
def post(self, request, *args, **kwargs):
customer_id = json.loads(request.body).get('customer_id')
queryset = customers.objects.filter(cid= customer_id)
serialize = customer_details(queryset, many=True)
return Response(serialize.data, status=status.HTTP_200_OK)
urls.py是这样的-
from django.conf.urls import include, url
from django.urls import path
from rest_framework import routers
from . import views
urlpatterns = [
url(r'^customer_location/$', views.customer_location.as_view(), name='customer_location'),
]
DEBUG 模式设置为 False,并在 ALLOWED_HOSTS 中添加 EC2 实例 IP 地址。
GET 方法工作正常,但 POST 方法出错。
根据 SENTRY ERROR LOG,问题是 -
raise JSONDecodeError("Expecting value", s, err.value) from None
这是 view.py 中的这一行 -
customer_id = json.loads(request.body).get('customer_id')
EC2 实例的入站规则允许对端口 8000 的 HTTP、HTTPS、SSH、PostgreSQL 和自定义 TCP 规则进行访问。
当我从终端运行 curl 命令时,我可以获得 POST 方法的值-
curl --header "Content-Type: application/json" --request POST --data '{"customer_id":"1"}' http://ec2_ip/customer_location/
output -
[{"cid":"1","name":"Rachel". "location":"NYC"}]
curl 命令返回同一 EC2 服务器的 POST 方法的值,而 EC2 服务器上的 api 端点返回 500 服务器错误。
我该如何解决这个问题?
【问题讨论】:
-
尝试启用 DEBUG 以查看实际错误是什么。
csrf浮现在脑海中。 -
错误日志没有显示任何内容。所以我将哨兵与应用程序集成在一起。哨兵显示错误 - raise JSONDecodeError("Expecting value", s, err.value) from None
-
@Seeker90 如果您有错误回溯,请将其添加到您的问题中。
-
@NalinDobhal - 嗨。我添加了哨兵的错误 - 它是从 None 引发 JSONDecodeError("Expecting value", s, err.value)
-
你使用了“django cors headers”吗?
标签: python django amazon-ec2 django-rest-framework internal-server-error