相关疑难解决方法(0)

在django-rest-framework中解析multipart/form-data

在使用django-rest-framework时,我在解析多部分表单数据时遇到了一些困难.我已经设置了一个最小视图来回显请求数据:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser, FormParser


class FileUpload(APIView):
    parser_classes = (MultiPartParser, FormParser, )

    def post(self, request, format=None, *args, **kwargs):
        return Response({'raw': request.data, 'data': request._request.POST,
                         'files': str(request._request.FILES)})
Run Code Online (Sandbox Code Playgroud)

我期待的是raw(略严重命名我承认)包含有效的相同数据request._request.POSTrequest._request.FILES.

如果我POST对视图的Content-Type= application/x-www-form-urlencoded工作符合预期:

$ http -v --form POST http://localhost:8000/upload/api/ course=3 name=name

POST /upload/api/ HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 18
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: localhost:8000
User-Agent: HTTPie/0.9.2

course=3&name=name

HTTP/1.0 200 OK
Allow: POST, …
Run Code Online (Sandbox Code Playgroud)

python django django-rest-framework

9
推荐指数
1
解决办法
4024
查看次数

标签 统计

django ×1

django-rest-framework ×1

python ×1