2014-02-07 2 views
1

django의 일반 뷰 (django.views.generic.base)를 사용하여 GET, POST, PUT 메서드를 구현하고 있습니다.Django PUT 메서드가 HttpResponse 개체를 반환하지 않음

GET 및 POST 메서드가 올바르게 작동합니다. 그러나 PUT 메서드는 HttpResponse 개체를 반환하지 않습니다. 서버에서 코드 200 OK이지만 데이터가 없습니다.

이것이 크로스 소스 공유 (CORS)와 관련된 문제 일 수 있다고 생각합니다.

누구든지 올바른 방향으로이 문제를 해결할 수 있습니까?

나는 deplyoment에 nginx + uwsgi를 사용하고 있습니다.


편집 : 나는 PUT 및 옵션 HTTP 동사를 처리 할 수의 nginx 서버를 사용할 수있다.

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") 
+0

보기의 코드를 게시 할 수 있습니까? – lai

+0

@lai :: 문제의 PUT 코드를 게시했습니다. – dotslash

답변

0

그래서, 첫째로, 당신의 DB 쿼리는 "맥"이 포함되어 있는지에 따라 보호되지 않습니다


여기 내 PUT 코드입니다. 이 문제를 해결하는 방법에 대한 자세한 내용은 How to use variables in SQL statement in Python?을 확인하십시오.

두 번째로 코드가 어디에 위치하는지 알고 있습니까?

"mac"kwarg가 메소드에 제대로 전달 되나요?

지금까지 내가 걱정하고, 나는 또한 예컨대 :

HttpResponse(json_dictionary, mimetype="application/json; charset=utf-8") 

아마도 그 도움이 될 수 있습니다, JSON 응답에서 명시 적 MIME 형식을 지정하는 데 사용할 수 있습니까?

관련 문제