Django PUT方法不返回HttpResponse对象
我正在使用django的泛型视图django.views.generic.base实现GET、POST和PUT方法 GET和POST方法工作正常。 然而,PUT方法并没有返回任何HttpResponse对象,尽管我从服务器上得到了一个代码200ok,但没有数据 我认为这可能是与跨来源资源共享CORS相关的问题 有人能给我指出解决这个问题的正确方向吗 我正在使用nginx+uwsgi进行deployment 编辑: 我已经启用了nginx服务器来处理PUT和OPTIONS http谓词 这是我的密码:Django PUT方法不返回HttpResponse对象,django,python-2.7,cross-domain,Django,Python 2.7,Cross Domain,我正在使用django的泛型视图django.views.generic.base实现GET、POST和PUT方法 GET和POST方法工作正常。 然而,PUT方法并没有返回任何HttpResponse对象,尽管我从服务器上得到了一个代码200ok,但没有数据 我认为这可能是与跨来源资源共享CORS相关的问题 有人能给我指出解决这个问题的正确方向吗 我正在使用nginx+uwsgi进行deployment 编辑: 我已经启用了nginx服务器来处理PUT和OPTIONS http谓词 这是我的密
def put(self, request, *args, **kwargs):
"""
Update from the controller with info that all the commands has
been successfully executed on that controller
:param mac:
:param request:
:param args:
:param kwargs:
"""
self.true_response["mac"] = None
self.false_response["mac"] = None
if request.method == 'PUT':
if "mac" in kwargs:
mac = kwargs["mac"]
self.true_response["mac"] = mac
self.false_response["mac"] = mac
query = "SELECT COUNT(1) FROM controller WHERE \
`controller_mac` = '%s'" % mac
cursor = connections['cnms'].cursor()
cursor.execute(query)
result = cursor.fetchall()
if not result[0][0]:
return HttpResponse(json.dumps(self.false_response))
try:
query = """ UPDATE command SET command_status = 2 WHERE \
command_mac '%s'""" % mac
cursor = connections['cnms'].cursor()
cursor.execute(query)
return HttpResponse(json.dumps(self.true_response))
except Exception as error:
return HttpResponse(json.dumps(self.false_response))
else:
return HttpResponse(json.dumps({"status" : "false"}))
else:
return HttpResponse("Method is Not Supported")
因此,首先,您的DB查询是不安全的,这取决于mac应该包含什么。 查看有关如何解决此问题的更多详细信息 其次,你知道你的代码分支在哪里吗 mac-kwarg是否正确地传递给该方法 就我而言,我还使用来明确指定json响应中的mimetype,例如:
HttpResponse(json_dictionary, mimetype="application/json; charset=utf-8")
也许这会有帮助?你能发布你观点的代码吗?@lai::我已经发布了有问题的PUT代码