Django TastyPie 0.10.0不接受POST请求

时间:2013-10-08 14:05:39

标签: django tastypie

我似乎无法让TastyPie接受通过Ajax制作的POST请求。我收到一个错误:

  

表示'multipart / form-data'的格式不可用   反序列化方法。请检查您的formats和   您的Serializer上的content_types

我的模型资源是:

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,因此我没有启用任何跨域发布中间件。我能够对端点/v2/api/clippedcoupon/执行GET请求就好了,但是POST完全失败了。我在Chrome中使用POSTMAN进行测试。谁能看到我做错了什么?

修改

我实施了cookie based authentication for TastyPie,一切正常。

1 个答案:

答案 0 :(得分:0)

在settings.py文件中的MIDDLEWARE_CLASSES中添加以下内容

MIDDLEWARE_CLASSES =(     'mysite.crossdomainxhr.XsSharing' )

复制此文件并将其与settings.py

放在同一级别
  • crossdomainxhr.py

来自django import 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'

类XsSharing(对象):     “””     该中间件允许使用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

这将有助于