修改头像
1、上传头像,我们需要的对它做一个forms验证,编辑users.forms.py
... from .models import UserProfile class UploadImageForm(forms.ModelForm): class Meta: model = UserProfile fields = [‘image‘]
2、编辑users.views.py
class UploadImageView(LoginRequiredMixin, View): def post(self, request): image_form = UploadImageForm(request.POST, request.FILES) if image_form.is_valid(): pass
3、配置url,编辑users.urls.py
... from .views import UploadImageView urlpatterns = [ ... url(r‘image/upload/$‘, UploadImageView.as_view(), name=‘upload_image‘), ]
4、编辑前端页面
5、断点调试
我们发现form已经取到上传的图片放到了cleaned_data中,这里面的数据是已经通过验证的。因此我们可以直接把它取出来保存到数据库中