在Django公处理请求参数,参数和文件整理到request.data和request.files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| from django.utils.deprecation import MiddlewareMixin import json
class DataMiddleware(MiddlewareMixin): """把request里面的的各种数据整理放在req.data里面"""
def process_request(self, request): data = {} content_type = request.META.get('CONTENT_TYPE', '') print(content_type) if 'application/json' in content_type: try: data = json.loads(request.body.decode()) except Exception as e: print('中间件解析错误', e) elif 'multipart/form-data' in content_type: try: data, files = request.parse_file_upload(request.META, request) request.files = files except Exception as e: print('中间件解析错误', e) elif 'application/x-www-form-urlencoded' in content_type: data = request.POST else: data = request.GET request.data = data print(request.method, request.data)
|