2022-10-03 16:57阅读: 25评论: 0推荐: 0

drf 解析器简单使用

默认解析器

JSONParser, FormParser, MultiPartParser

view

class UserView(OrPermissionAPIView):
    from rest_framework.parsers import JSONParser, FormParser
    from rest_framework.negotiation import DefaultContentNegotiation
    parser_classes = [JSONParser, FormParser]
    content_negotiation_class = DefaultContentNegotiation

    def post(self, request, *args, **kwargs):
        # 获取url里的
        print(request.query_params, type(request.query_params))
        # 获取body里的
        print(request.data, type(request.data))
        return Response('post')

application/json

image

application/x-www-form-urlencoded

image

获取文件

class UserView(OrPermissionAPIView):
    from rest_framework.parsers import MultiPartParser
    from rest_framework.negotiation import DefaultContentNegotiation
    parser_classes = [MultiPartParser]
    content_negotiation_class = DefaultContentNegotiation

    def post(self, request, *args, **kwargs):
        file = request.data.get('data1')
        with open(file.name,mode='wb') as target:
            for chunk in file:
                target.write(chunk)

        return Response('post')

image

本文作者:Sherwin

本文链接:https://www.cnblogs.com/sherwin1995/p/16750770.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Sherwin_szw  阅读(25)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.