我有一个表格,用户可以选择自己的个人资料图片并写自己的简历。表单应采用上传的图片并将其尺寸调整为200x200。它会上传图片,但不会进行大小调整。我尝试了以下this tutorial的操作:但是,由于某些原因,我的图像在上载时仍无法调整大小。这是我的代码:
class ProfileForm(ModelForm):
avatar = forms.ImageField(required=False, widget=forms.FileInput)
bio = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, "placeholder": "Bio"}), max_length=200,
required=False)
class Meta:
model = Profile
fields = ['avatar', 'bio', 'gender'']
def save(self, *args, **kwargs):
profile = super(ProfileForm, self).save()
image = Image.open(profile.avatar)
resized_image = image.resize((200, 200), Image.ANTIALIAS)
resized_image.save(profile.avatar.path)
return profile
class Profile(models.Model):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
bio = models.CharField(max_length=200, null=True)
avatar = models.ImageField(storage=OverwriteStorage(), upload_to=create_user_image_path)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True)
正在上传图片,并且一切正常,但是,图片没有调整大小。