我试图更改图像的大小,但是当我运行代码时,它显示此错误:`AttributeError:模块'PIL.Image'没有属性'_meta' 这是我的models.py文件:
from django.db import models
from PIL import Image
class LmtbProject(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
public = models.BooleanField(default=True)
LmtbImage = models.ImageField(blank=True)
def __str__(self):
return f'{self.title}'
def save(self):
super().save()
img = Image.open(self.LmtbImage.path)
output_size = (1000, 800)
img.thumbnail(output_size)
img.save(self.LmtbImage.path)
class SecondaryImage(models.Model):
MyProject = models.ForeignKey(LmtbProject, default=None, on_delete=models.CASCADE)
image = models.ImageField(upload_to='images/')
def __str__(self):
return f'{self.MyProject.title}'
def save(self):
super().save()
img = Image.open(self.image.path)
output_size = (1000, 800)
img.thumbnail(output_size)
img.save(self.image.path)
`这是我的看法:
def project(request):
projects = LmtbProject.objects.all()
context = {
'projects': projects
}
return render(request, 'projects.html', context)
def project_detail(request, id):
project = get_object_or_404(LmtbProject, id=id)
photos = SecondaryImage.objects.filter(MyProject=project)
return render(request, 'project-detail.html', {'project': project, 'photos': photos})