2013-10-08 67 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