def post(self, request, *args, **kwargs): image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return JsonResponse({'status': 'success'}) else: return JsonResponse({'status': 'fail'})
def post(self, request): """修改头像""" # 使用forms表单上传头像 upload_image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if upload_image_form.is_valid(): upload_image_form.save() return JsonResponse({'status': 'success'}) else: return JsonResponse({"status": 'fail'})
def post(self, request, *args, **kwargs): #处理用户上传的头像 image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return JsonResponse({"status": "success"}) else: return JsonResponse({"status": "fail"})
def post(self, request, *args, **kwargs): # 读取图片,并将图片保存(不使用ModelForm如何处理用户上传的头像) image_form = UploadImageForm( request.POST, request.FILES, instance=request.user ) # instance=request.user 表示修改的是哪一个用户 request.FILES修改的哪个字段 if image_form.is_valid(): image_form.save() return JsonResponse({"status": "success"}) else: return JsonResponse({"status": "fail"})
def post(self, request): image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return HttpResponse('{"status":"success"}', content_type='application/json') else: return HttpResponse('{"status":"fail"}', content_type='application/json')
def post(self, request): # 上传的文件都在request.FILES里面获取,所以这里要多传一个参数 image_form = UploadImageForm(request.POST, request.FILES) if image_form.is_valid(): image = image_form.cleaned_data['image'] request.user.image = image request.user.save() return HttpResponse('("status":"success")', content_type='application/json') else: return HttpResponse('{"status":"fail"}', content_type='application/json')
def post(self, request, *args, **kwargs): # 把用户上传的图片交给modelForm处理,并且要指明当前的用户对象实例是哪一个 image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return JsonResponse({ "status": "success", }) else: return JsonResponse({ "status": "fail", })
def post(self, request, *args, **kwargs): #处理用户上传的头像 #1.如果一个文件上传多次,同一名称的文件如何处理 #2.文件的保存路径应该写入user #3.做表单验证 image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return JsonResponse({"status": "success"}) else: return JsonResponse({"status": "fail"})
def post(self, request, *args, **kwargs): # 处理用户上传的头像 # files = request.FILES['image'] # self.save_file(files) image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return JsonResponse({ "status": 'success', }) else: return JsonResponse({ "status": 'fail', })
def post(self, request): # image_form = UploadImageForm(request.POST, request.FILES) # if image_form.is_valid(): # image = image_form.cleaned_data['image'] # request.user.image = image # request.user.save() image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) res = dict() if image_form.is_valid(): image_form.save() res['status'] = 'success' res['msg'] = '头像修改成功' else: res['status'] = 'fail' res['msg'] = '头像修改失败' return HttpResponse(json.dumps(res), content_type='application/json')
def post(self, request): # 普通Form # image_form = UploadImageForm(request.POST, request.FILES) # if image_form.is_valid(): # image = image_form.cleaned_data['image'] # request.user.image = image # request.user.save() # 使用model form image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return HttpResponse('{"status":"success"}', content_type='application/json') else: return HttpResponse('{"status":"fail"}', content_type='application/json')
def post(self, request, *args, **kwargs): # 处理用户上传的头像 # files = request.FILES["image"] # self.save_file(files) """问题 1. 如果同一个文件上传多次,相同名称的文件应该如何处理 2. 文件的保存路径应该写入到user 3. 还没有做表单验证 """ image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() return JsonResponse({ "status": "success" }) else: return JsonResponse({ "status": "fail" })
def post(self, request, *args, **kwargs): image_form = UploadImageForm(request.POST, request.FILES, instance=request.user) if image_form.is_valid(): image_form.save() messages.info(request, "头像修改成功!") message = UserMessage() message.user = request.user message.messgae = "用户头像修改成功!" message.has_read = False message.save() return JsonResponse({ "status": "success", }) else: messages.error(request, "头像修改失败!") return JsonResponse({ "status": "fail", })