def process_request(self, request): #判断是否使用的是get方法,直接返回 method = request.method if method == 'GET': return None if 'application/json' in request.content_type: try: data = json.loads(request.body.decode()) except Exception: return params_error({"msg": "json数据格式有误"}) else: return params_error({"msg": "请提交json数据类型"}) setattr(request, method, data)
def process_request(self, request): method = request.method if 'application/json' in request.content_type: # 把客户端上传的json数据转化成python字典 try: data = json.loads(request.body.decode()) files = None except Exception as e: return params_error({'body': '请求数据类型不正确'}) elif 'multipart/form-data' in request.content_type: # 把客户端已formdata上传的数据进行解析,通常客户端会把上传的文件也放在formdata中, # 所以下面的解析会把上传的文件也解析出来 data, files = MultiPartParser(request.META, request, request.upload_handlers).parse() else: data = request.GET files = None if 'HTTP_X_METHOD' in request.META: method = request.META['HTTP_X_METHOD'].upper() setattr(request, 'method', method) if files: setattr(request, '{method}_FILES'.format(method=method), files) setattr(request, method, data)
def put(self,request,*args,**kwargs): # print('请求的数据类型是:') # print(request.content_type) # print('http 请求体是:') # data = json.loads(request.body.decode()) # print(data) data=request.PUT # print(data) username = data.get('username','') password = data.get('password','') ensure_password = data.get('ensure_password','') regist_code = data.get('regist_code',0) session_regist_code = request.session.get('regist_code',1) error = dict() if not username: error['username'] = "******" else: if User.objects.filter(username=username).count()>0: error['username'] = '******' if len(password)<6: error['password'] = '******' if password != ensure_password: error['ensure_password'] = '******' if regist_code != session_regist_code: error['regist_code'] = '验证码不匹配' if error: return params_error(error) user = User() user.username = username user.set_password(password) user.save() #category = data.get('category','userinfo') category = data.get('category', 'customer') if category == 'userinfo': #创建普通用户 user_obj = UserInfo() user_obj.name = "" user_obj.qq = "" else: #创建客户 user_obj = Customer() user_obj.name = "" user_obj.email="" user_obj.user = user user_obj.save() #print(request.PUT) #{'username': '******', 'password': '******'} return json_response({"注册的id:":user.id})
def put(self,request,*args,**kwargs): data = request.PUT username = data.get('username','') password = data.get("password",'') #查询数据库用户表 user = authenticate(username=username,password=password) if user: #保存登陆状态 login(request,user) return json_response( { "msg":"登录成功" } ) else: return params_error({ "msg":"用户名或者密码错误" })
def user(request): method = request.method if method == 'GET': # 获取用户信息 return json_response({}) elif method == 'POST': # 更新用户信息 return json_response({}) elif method == 'PUT': # 注册用户 data = request.PUT username = data.get('username', '') password = data.get('password', '') ensure_password = data.get('ensure_password', '') regist_code = data.get('regist_code', '') session_regist_code = request.session.get('regist_code', False) errors = dict() if User.objects.filter(username=username): errors['username'] = '******' if password < 6: errors['password'] = '******' if password != ensure_password: errors['ensure_password'] = '******' if regist_code != session_regist_code: errors['ensure_password'] = '******' if errors: return params_error(errors) user = User.objects.create(username=username, password=password) user.save() login(request, user) return json_response({ 'state': 200, 'msg': '参数不正确', 'data': {'id': user.id} }) else: return method_not_allowed()
def process_request(self, request): method = request.method if 'application/json' in request.content_type: try: data = json.loads(request.body.decode()) files = None except Exception as e: return params_error({'body': '请求数据类型不正确'}) elif 'application/x-www-form-urlencoded' in request.content_type: data = request.POST files = None elif 'multipart/form-data' in request.content_type: data = request.POST # data, files = MultiPartParser( # request.META, request, request.upload_handlers).parse() else: data = request.GET files = None if 'HTTP_X_METHOD' in request.META: method = request.META['HTTP_X_METHOD'].upper() setattr(request, 'method', method) setattr(request, method, data)