0

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

指示“multipart/form-data”的格式没有可用的反序列化方法。请检查您的formatscontent_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,所以我还没有启用任何跨域发布中间件。我能够对端点执行 GET 请求,/v2/api/clippedcoupon/很好,但是 POST 完全失败。我在 Chrome 中使用 POSTMAN 进行测试。谁能看到我做错了什么?

编辑:

为 TastyPie 实现了基于 cookie 的身份验证,一切都按预期工作。

4

1 回答 1

0

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

MIDDLEWARE_CLASSES = ('mysite.crossdomainxhr.XsSharing')

复制此文件并将其放在与 settings.py 相同的级别

  • 跨域xhr.py

从 django 导入 http

try: from django.conf import settings 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 except 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): """ 这个中间件允许跨域 XHR 使用 html5 postMessage API。

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

这会有所帮助

于 2013-10-08T14:10:34.737 回答