我正在使用django的通用视图(django.views.generic.base)实现GET,POST,PUT方法。
GET和POST方法工作正常。但是,PUT方法没有返回任何HttpResponse对象,尽管我从服务器获取了代码200 OK,但没有数据。
我认为这可能是与跨源资源共享(CORS)相关的问题。
有人可以指出正确的方向来解决此问题吗?
我正在使用nginx + uwsgi进行毁灭。
编辑:我已启用Nginx服务器来处理PUT和OPTIONS http动词。
这是我的PUT代码:
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")
因此,首先,您的数据库查询不安全,这取决于应该包含的“ mac”。请查看How to use variables in SQL statement in Python?,以获取有关如何解决此问题的更多详细信息。
其次您知道代码在哪里分支吗?
“ mac” kwarg是否正确传递给该方法?
就我而言,我还使用了json响应中明确指定mimetype,例如:
HttpResponse(json_dictionary, mimetype="application/json; charset=utf-8")
也许有帮助?