def regist(req): if req.method == 'GET': return render(req, 'register.html') user = User() user.username = req.POST.get('username') user.passwd = crypt(req.POST.get('passwd')) user.token = newToken(user.username) user.photo = req.FILES['photo'] uploadFile = req.FILES.get('photo') saveFileName = newFileName(uploadFile.content_type) saveFilePath = os.path.join(settings.MEDIA_ROOT, saveFileName) with open(saveFilePath, 'wb') as f: for part in uploadFile.chunks(): f.write(part) f.flush() user.img = saveFilePath user.token = newToken(user.username) user.save() resp = redirect('/main/login') resp.set_cookie('token', user.token) return resp
def upload(req): # 上传文件 uploadFile = req.FILES.get('photo') print(uploadFile.__dict__) # content_type user = User() saveFileName = str(uuid.uuid4()) + ".jpg" saveFilePath = os.path.join(settings.MEDIA_ROOT, saveFileName) with open(saveFilePath, 'wb') as f: for part in uploadFile.chunks(): f.write(part) f.flush() user.img = saveFilePath # user.token = newToken(user.userName) user.save() # resp = HttpResponseRedirect('/app/home') # resp.set_cookie('token', user.token) # return resp return JsonResponse({"path": saveFileName})