检查了许多其他答案,并没有找到我要找的东西!我正在处理Django项目,我必须让用户上传照片,现在我需要调整照片大小以适应将要显示的地方
这是我的档案:
模型
class Item(models.Model):
# custom validators
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', 'Only alphanumeric characters are allowed.')
# fields
dress_name = models.ForeignKey(Name, on_delete='DO_NOTHING', null=False, verbose_name='وصف الفستان',)
dress_size = models.ForeignKey(Size, on_delete='DO_NOTHING', verbose_name='مقاس الفستان')
dress_color = models.ForeignKey(Color, on_delete='DO_NOTHING', verbose_name='لون الفستان')
dress_image1 = models.FileField(upload_to='documents/%Y/%m/%d', null=False, verbose_name='الصورة الأساسية للفستان',
help_text='لا يمكنك تركها فارغة',)
dress_image2 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, verbose_name='صورة إضافية ')
dress_image3 = models.ImageField(upload_to='documents/%Y/%m/%d', null=True, verbose_name='صورة إضافة ')
dress_action = models.ForeignKey(Action, on_delete='DO_NOTHING', verbose_name='الفستان معروض لل ',
help_text='للبيع او للإيجار ')
dress_price = models.IntegerField(default=1, verbose_name='السعر')
dress_country = models.ForeignKey(Country, on_delete='CASCADE', verbose_name='البلد')
dress_city = models.CharField(max_length=80, verbose_name='المدينة او المحافظة')
dress_mobile = models.CharField(max_length=20, validators=[alphanumeric], verbose_name='رقم الهاتف ')
created_by = models.CharField(max_length=250,)
created_username = models.CharField(max_length=250, default='unknown')
created_at = models.DateTimeField(auto_now=True)
dress_active = models.BooleanField(default=False)
dress_special = models.BooleanField(default=False)
形式
class AddDressForm(ModelForm):
class Meta:
model = Item
exclude = ['created_by', 'created_at','dress_active','dress_special','created_username']
def clean_image(self):
image = self.cleaned_data.get('image', False)
if image:
if image._size > 4*1024*1024:
raise ValidationError("Image file too large ( > 4mb )")
return image
else:
raise ValidationError("Couldn't read uploaded image")
最后观看次数
def dress_add(request):
all_name = Name.objects.all()
current_user = request.user
if request.method == "POST":
add_dress_form = AddDressForm(request.POST, request.FILES)
if add_dress_form.is_valid():
model_instance = add_dress_form.save(commit=False)
model_instance.created_by = current_user.first_name
model_instance.created_username = current_user.username
if model_instance.dress_image1:
model_instance.dress_image1 = Image.open(model_instance.dress_image1)
model_instance.dress_image1.resize((1000, 1000), Image.ANTIALIAS)
thumb_io = BytesIO()
model_instance.dress_image1.save(thumb_io, model_instance.format, quality=60 )
model_instance.image.save(model_instance.filename, ContentFile(thumb_io.get_value()), save=False)
model_instance.save()
return redirect('dress_confirm')
else:
add_dress_form = AddDressForm()
context = {
'add_dress_form':add_dress_form,
'current_user':current_user,
'all_name':all_name,
}
return render(request, 'fostan/dress_add.html', context)
我尝试在PIL lib中使用resize方法调整图片大小但是它给了我一个错误
错误
`'项目'对象没有属性'格式'
部分解决了
1 - 更改了model_instance.dress_image1.save(thumb_io, model_instance.format, quality=60 )
就像:
model_instance.dress_image1.save(thumb_io, model_instance.dress_image1.format, quality=60 )
2 - 然后将model_instance.filename
更改为model_instance.dress_image1.filename
`
3 - 然后改变了model_instance.image.save(model_instance.filename, ContentFile(thumb_io.get_value()), save=False)
是`model_instance.dress_image1.save(model_instance.dress_image1.filename,ContentFile(thumb_io.getvalue()),save = False)
错误
然后我现在有一个新错误
'ContentFile' object has no attribute 'upper'
答案 0 :(得分:1)
问题在于这一行:
model_instance.dress_image1.save(thumb_io, model_instance.format, quality=60 )
你可能想要这个:
model_instance.dress_image1.save(thumb_io, model_instance.dress_image1.format, quality=60 )