2013-10-08 97 views
0

我似乎無法讓TastyPie接受通過Ajax製作的POST請求。我得到一個錯誤:Django TastyPie 0.10.0不接受POST請求

The format indicated 'multipart/form-data' had no available deserialization method. Please check your formats and content_types on your Serializer.

我的模型資源:

class ClippedCouponResource(ModelResource): 
    class Meta: 
     queryset = ClippedCoupon.objects.all() 
     allowed_methods = ['get', 'post'] 
     serializers = UrlencodeSerializer() 
     authentication = DjangoCookieBasicAuthentication() 
     authorization = DjangoAuthorization() 
     default_format = 'application/json' 

我的串行是:

from urlparse import urlparse 

from tastypie.serializers import Serializer 


class UrlencodeSerializer(Serializer): 
    formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode'] 
    content_types = { 
     'json': 'application/json', 
     'jsonp': 'text/javascript', 
     'xml': 'application/xml', 
     'yaml': 'text/yaml', 
     'html': 'text/html', 
     'plist': 'application/x-plist', 
     'urlencode': 'application/x-www-form-urlencoded', 
    } 

    def from_urlencode(self, data, options=None): 
     """ handles basic formencoded url posts """ 
     qs = dict((k, v if len(v) > 1 else v[0]) 
      for k, v in urlparse.parse_qs(data).iteritems()) 
     return qs 

    def to_urlencode(self,content): 
     pass 

現在,我只是在當地的發展模式,因此所有的請求打算localhost:8000,所以我沒有啓用任何跨域發佈中間件。我能夠對端點執行GET請求,/v2/api/clippedcoupon/就好,但是POST完全失敗。我在Chrome中使用POSTMAN進行測試。任何人都可以看到我做錯了什麼?

編輯:

我實現cookie based authentication for TastyPie,一切工作正常。

回答

0

附加繼MIDDLEWARE_CLASSES在你的settings.py文件

MIDDLEWARE_CLASSES =( 'mysite.crossdomainxhr.XsSharing' )

複製這個文件並把它放在同一水平settings.py

  • 從Django的小鬼crossdomainxhr.py

ORT HTTP

嘗試: 從django.conf導入設置 XS_SHARING_ALLOWED_ORIGINS = settings.XS_SHARING_ALLOWED_ORIGINS XS_SHARING_ALLOWED_METHODS = settings.XS_SHARING_ALLOWED_METHODS XS_SHARING_ALLOWED_HEADERS = settings.XS_SHARING_ALLOWED_HEADERS XS_SHARING_ALLOWED_CREDENTIALS = settings.XS_SHARING_ALLOWED_CREDENTIALS 除AttributeError的: XS_SHARING_ALLOWED_ORIGINS = '*' XS_SHARING_ALLOWED_METHODS = ['POST','GET','OPTIONS','PUT','DELETE','PATCH'] XS_SHARING_ALLOWED_HEADERS = ['Content-Type','*'] XS_SHARING_ALLOWED_CREDENTIALS ='true'

class XsSharing(object): 「」「 該中間件允許使用html5 postMessage API的跨域XHR。

Access-Control-Allow-Origin: http://foo.example 
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE 

Based off https://gist.github.com/426829 
""" 
def process_request(self, request): 
    if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META: 
     response = http.HttpResponse() 
     response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS 
     response['Access-Control-Allow-Methods'] = ",".join(XS_SHARING_ALLOWED_METHODS) 
     response['Access-Control-Allow-Headers'] = ",".join(XS_SHARING_ALLOWED_HEADERS) 
     response['Access-Control-Allow-Credentials'] = XS_SHARING_ALLOWED_CREDENTIALS 
     return response 

    return None 

def process_response(self, request, response): 
    response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS 
    response['Access-Control-Allow-Methods'] = ",".join(XS_SHARING_ALLOWED_METHODS) 
    response['Access-Control-Allow-Headers'] = ",".join(XS_SHARING_ALLOWED_HEADERS) 
    response['Access-Control-Allow-Credentials'] = XS_SHARING_ALLOWED_CREDENTIALS 

    return response 

這將有助於

+0

不幸的是,我得到了同樣的錯誤。這裏是完整的回溯:https://gist.github.com/btaylordesign/6885415/raw/29f167760a2fc2c3ec119b9ac1c0123e936b4b98/gistfile1.txt – Brandon

+0

@Brandon plz給我你的JSON Documnet,你發送到POST您的消息顯示數據格式不正確 –

+0

它很簡單:{'coupon_id':1,'user_id':1} – Brandon