1. 程式人生 > >修改密碼和頭像10-4

修改密碼和頭像10-4

1.view書寫
class UploadImageView(LoginRequiredMixin, View):
“”"
使用者修改頭像
“”"

方法1
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()
        pass

// request.FILES上傳檔案需要配置的
//image_form.cleaned_data[‘image’],form會將驗證通過的欄位放在裡面
方法2
運用ModelForm的性質

  • def post(self, request):

     image_form = UploadImageForm(request.POST, request.FILES,instance=request.user)
     if image_form.is_valid():
         image_form.save()
         pass
    

2.form表單定義

class UploadImageForm(forms.ModelForm):

class Meta:
    model = UserProfile
    fields = ['image']   

3. html修改
4. 新增url

url(r’^image/upload/$’, UploadImageView.as_view(), name=‘image_upload’),