def register(request): result = {'status': 'success'} if request.method == 'POST': print('>>>注册用户') formData = CMDBUserForm(data=request.POST, files=request.FILES) print('>>>>>>>>>>>>>: ', formData.is_valid()) if formData.is_valid(): try: cleanData = formData.cleaned_data user = CMDBUser() user.username = cleanData.get('username') user.password = cleanData.get('password') user.nickname = cleanData.get('nickname') user.phone = cleanData.get('phone') user.email = cleanData.get('email') user.photo = cleanData.get('photo').name user.save() files = request.FILES.get('photo') savePath = os.path.join(BASE_DIR + '\\static\\images\\' + files.name) with open(savePath, 'wb') as f: for chunk in files.chunks(): f.write(chunk) except Exception as e: print('AAAAAAAA: ', e) return JsonResponse(result) else: result['status'] = 'error' return JsonResponse(result)
def index(request): forms = CMDBUserForm() if request.method == "POST" and request.POST and request.FILES: formsData = CMDBUserForm(data=request.POST, files=request.FILES) if formsData.is_valid(): requestData = formsData.cleaned_data username = requestData.get('username') password = requestData.get('password') nickname = requestData.get('nickname') phone = requestData.get('phone') email = requestData.get('email') photo = requestData.get('photo').name #实例化数据库,保存数据 user = CMDBUser() user.username = username user.password = password user.nickname = nickname user.phone = phone user.email = email user.photo = photo user.save() #保存文件,没有限制文件格式 photofile = request.FILES.get('photo') path = os.path.join(BASE_DIR, 'static\\images\\%s' % photofile.name) with open(path, 'wb') as f: for chunk in photofile.chunks(): f.write(chunk) else: print(formsData.errors) return render(request, 'index.html', locals())
def register(request): # if request.method == 'POST': # return JsonResponse({"method":'POST'}) # # else: # return JsonResponse({"method":"GET"}) result = {"submit": 'success'} #判断三个,请求的方法,post请求的内容和,请求的文件 #判断请求方式是否为post,request.POST的意思是判断请求是否有数据,request。files是判断上传是否有文件 if request.method == 'POST' and request.POST and request.FILES: register_data = CMDBUserForm(data=request.POST, files=request.FILES) if register_data.is_valid(): #检验成功 register_post_data = register_data.cleaned_data username = register_post_data.get('username') password = register_post_data.get('password') nickname = register_post_data.get('nickname') phone = register_post_data.get('phone') email = register_post_data.get('email') #当去get图片的时候的导师是一个对象,用。name是获取photo的值 photo = register_post_data.get('photo').name CMDBUser.objects.create( username=username, password=password, nickname=nickname, phone=phone, email=email, photo=photo, ) photofile = request.FILES.get('photo') photoSavePath = os.path.join(BASE_DIR, 'media/images/%s' % photofile.name) with open(photoSavePath, 'wb') as pf: for chunk in photofile.chunks(): pf.write(chunk) return JsonResponse(result) else: result["submit"] = "failed" print(register_data.errors) return JsonResponse(result) else: return JsonResponse({"method": 'GET'})
def index(request): forms = CMDBUserForm() # 定义form表单 if request.method == "POST" and request.POST and request.FILES: # 判断 # 1、请求方式是post # 2、post请求有内容 # 3、文件请求有内容 formsData = CMDBUserForm(data=request.POST, files=request.FILES) # 接收回来的数据 # 校验提交的数据和文件 if formsData.is_valid(): requestData = formsData.cleaned_data username = requestData.get("username") # get里面的是前端input里面的name password = requestData.get("password") nickname = requestData.get("nickname") phone = requestData.get("phone") email = requestData.get("email") photo = requestData.get("photo").name # 这里获取到的不是一个值,而是一个文件对象 # 存入数据库的应该是一个名字 user = CMDBUser() # 实例化数据库,保存数据,存入数据库 user.username = username user.password = password user.nickname = nickname user.phone = phone user.email = email user.photo = photo user.save() # 保存文件,这里没有限制文件格式,上传啥都行 photofile = request.FILES.get("photo") path = os.path.join(BASE_DIR, "static\\images\\%s" % photofile.name) with open(path, "wb") as f: for chunk in photofile.chunks(): # 图片的解析阅读方式 f.write(chunk) else: print(formsData.errors) return render(request, 'index.html', locals())
def register(request): result = {"submit": "success"} if request.method == "POST" and request.POST and request.FILES: formsData = CMDBUserForm(data=request.POST, files=request.FILES) if formsData.is_valid(): requestData = formsData.cleaned_data username = requestData.get('username') password = requestData.get('password') nickname = requestData.get('nickname') phone = requestData.get('phone') email = requestData.get('email') photo = requestData.get('photo').name #实例化数据库,保存数据 user = CMDBUser() user.username = username user.password = password user.nickname = nickname user.phone = phone user.email = email user.photo = photo user.save() #保存文件,没有限制文件格式 photofile = request.FILES.get('photo') path = os.path.join(BASE_DIR, 'static\\images\\%s' % photofile.name) with open(path, 'wb') as f: for chunk in photofile.chunks(): f.write(chunk) return JsonResponse(result) else: print(formsData.errors) result["submit"] = "error" return JsonResponse(result) else: return JsonResponse({"method": "GET"})
def index(request): if request.method == 'POST': return JsonResponse({'status': 'success'}) else: forms = CMDBUserForm() return render(request, 'index.html', locals())