def save(self,request): if self.cleaned_data['city'] or self.cleaned_data['website'] or self.cleaned_data['intro'] or self.cleaned_data['qq'] or self.cleaned_data['msn'] or self.cleaned_data['avatar']: current_user = request.user try: profile = current_user.get_profile() except UserProfile.DoesNotExist: profile = None if not profile: profile = UserProfile() profile.user = current_user profile.song_ord_filed = 'post_datetime' profile.city = self.cleaned_data['city'] profile.website = self.cleaned_data['website'] profile.intro = self.cleaned_data['intro'] profile.qq = self.cleaned_data['qq'] profile.msn = self.cleaned_data['msn'] if 'avatar' in request.FILES: #删除掉原头像 import os if profile and profile.avatar and os.path.exists(profile.avatar.path): os.remove(profile.avatar.path) profile.avatar = self.cleaned_data['avatar']#request.FILES["avatar"] profile.save() if self.cleaned_data['username'] != current_user.username: current_user.username = self.cleaned_data['username'] current_user.save()
def loadfile(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): adminuser = User.objects.get(id=1) newuser = UserProfile() newuser.avatar = form.cleaned_data['userpic'] newuser.user_id = adminuser.id newuser.accepted_eula = True newuser.favorite_animal = '*****@*****.**' newuser.save() return HttpResponse('ok!') else: return HttpResponse('parameters is wronngs!')
def user_center_avatar(request): user = request.user user_profile = UserProfile.objects.filter(user=user).first() if request.method == 'POST': img = request.FILES.get('avatar', None) #可以在这里删除名字有覆盖的头像,但是有个问题,不同用户上传同名不同图片的头像就歇逼了 #看看可不可以在储存头像的时候对头像名称进行处理,加上用户的id #可以是可以,用f.open存储头像自定义图片名字,然后向avatar中存入f #不过貌似比较麻烦,以后有需要再说吧 if user_profile: user_profile.avatar = img user_profile.save() else: user_profile = UserProfile() user_profile.user = user user_profile.avatar = img user_profile.save() return render(request, 'user_manage/user_center_avatar.html', {'user_profile': user_profile})
def save(self, request): if self.cleaned_data['city'] or self.cleaned_data[ 'website'] or self.cleaned_data['intro'] or self.cleaned_data[ 'qq'] or self.cleaned_data['msn'] or self.cleaned_data[ 'avatar']: current_user = request.user try: profile = current_user.get_profile() except UserProfile.DoesNotExist: profile = None if not profile: profile = UserProfile() profile.user = current_user profile.song_ord_filed = 'post_datetime' profile.city = self.cleaned_data['city'] profile.website = self.cleaned_data['website'] profile.intro = self.cleaned_data['intro'] profile.qq = self.cleaned_data['qq'] profile.msn = self.cleaned_data['msn'] if 'avatar' in request.FILES: #删除掉原头像 import os if profile and profile.avatar and os.path.exists( profile.avatar.path): os.remove(profile.avatar.path) profile.avatar = self.cleaned_data[ 'avatar'] #request.FILES["avatar"] profile.save() if self.cleaned_data['username'] != current_user.username: current_user.username = self.cleaned_data['username'] current_user.save()