我在我的模型中创建了缩略图,这个函数返回缩略图的动态路径 url。现在我将如何在我的模板中使用它?我必须编写一些视图函数吗?这是我的models.py
def img_file_upload_path(instance, filename):
""" creates unique-Path & filename for upload """
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return os.path.join(
'images','eventpic','original', instance.album_id.event_id.channel_id.publisher.user.username, instance.album_id.event_id.channel_id.channel_title, instance.album_id.event_id.event_title,instance.album_id.name,filename
#images/ john/ johnchannel/ birthday/ img1.jpg
)
def formatted_img_file_upload_path(instance, filename):
""" creates unique-Path & filename for upload """
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
PATH = django_settings.MEDIA_ROOT+ os.path.join(
'images','eventpic','formatted', instance.album_id.event_id.channel_id.publisher.user.username, instance.album_id.event_id.channel_id.channel_title, instance.album_id.event_id.event_title,instance.album_id.name
#images/ john/ johnchannel/ birthday/ img1.jpg
)
#print PATH
#print filename
if not os.path.exists(PATH) :
os.makedirs(django_settings.MEDIA_ROOT + os.path.join(
'images','eventpic','formatted', instance.album_id.event_id.channel_id.publisher.user.username, instance.album_id.event_id.channel_id.channel_title, instance.album_id.event_id.event_title,instance.album_id.name
#images/ john/ johnchannel/ birthday/ img1.jpg
))
return PATH +"/" +filename
def thumb_img_file_upload_path(instance, filename):
""" creates unique-Path & filename for upload """
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
PATH = django_settings.MEDIA_ROOT+ os.path.join(
'images','eventpic','thumb', instance.album_id.event_id.channel_id.publisher.user.username, instance.album_id.event_id.channel_id.channel_title, instance.album_id.event_id.event_title,instance.album_id.name
#images/ john/ johnchannel/ birthday/ img1.jpg
)
#print PATH
#print filename
if not os.path.exists(PATH) :
os.makedirs(django_settings.MEDIA_ROOT + os.path.join(
'images','eventpic','thumb', instance.album_id.event_id.channel_id.publisher.user.username, instance.album_id.event_id.channel_id.channel_title, instance.album_id.event_id.event_title,instance.album_id.name
#images/ john/ johnchannel/ birthday/ img1.jpg
))
return PATH +"/" +filename
class Photo(models.Model):
image_id = models.AutoField(primary_key=True)
uuid = UUIDField(auto=True)
album_id = models.ForeignKey(Album,db_column='album_id')
title = models.CharField(max_length=255)
summary = models.TextField(blank=True, null=True)
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
is_cover_photo = models.BooleanField()
photo = models.ImageField(upload_to=img_file_upload_path,max_length=500)
def get_model_fields(self):
return model._meta.fields
def thumb_image(self,img):
#Image thumbnail code starts here
img.thumbnail((140,100), Image.ANTIALIAS)
thumb = img.save(thumb_img_file_upload_path(self, self.photo.path),quality=90)
#Image resizing code ends here
return thumb
def formatted_image(self,img):
# Image resizing code starts here
size=(1200, 840)
pw = self.photo.width
ph = self.photo.height
nw = size[0]
nh = size[1]
if pw > nw or ph > nh:
# photo aspect is wider than destination ratio
image = ImageOps.fit(img,(nw, nh), Image.ANTIALIAS,(0.5, 0.5))
else:
# photo aspect matches the destination ratio
image = ImageOps.fit(img, (pw, ph), Image.ANTIALIAS, (0.5, 0.5))
formatted = image.save(formatted_img_file_upload_path(self, self.photo.path),quality=90)
return formatted
def save(self):
super(Photo,self).save()
if self.photo:
filename = img_file_upload_path(self, self.photo.path)
if self.is_cover_photo:
other_cover_photo = Photo.objects.filter(album_id=self.album_id).filter(is_cover_photo = True)
for photo in other_cover_photo:
photo.is_cover_photo = False
#photo.save()
if not filename == '':
img = Image.open(self.photo.path)
if img.mode not in ("L", "RGB"):
img = img.convert("RGB")
self.formatted_image(img)
self.thumb_image(img)
def get_formatted_image(self,filename):
""" creates unique-Path & filename for upload """
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return os.path.join(
'images','eventpic','formatted', self.album_id.event_id.channel_id.publisher.user.username, self.album_id.event_id.channel_id.channel_title, self.album_id.event_id.event_title,self.album_id.name,filename
#images/ john/ johnchannel/ birthday/ img1.jpg
)
def get_thumb_image(self, filename):
""" creates unique-Path & filename for upload """
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return os.path.join(
'images','eventpic','thumb', self.album_id.event_id.channel_id.publisher.user.username, self.album_id.event_id.channel_id.channel_title, self.album_id.event_id.event_title,self.album_id.name,filename
#images/ john/ johnchannel/ birthday/ img1.jpg
)
这是我的views.py
def imageview(request,album_id):
e_piclist = Photo.objects.filter(album_id = album_id).only('photo')
formatted_photo = Photo.get_formatted_image(???????)
thumb_photo = Photo.get_thumb_image(????????)
return render_to_response('gallery/image.html',
{
'e_piclist' : e_piclist,
'formatted_photo' : formatted_photo,
'thumb_photo' : thumb_photo,
},context_instance=RequestContext(request))
我卡在这里..我需要显示一个相册下的所有图像..我得到了原始图像,但无法调用 thumb 和格式化大小,因为它们在 Photo 类的函数中返回图像路径并接收 2 个参数。我现在应该怎么做才能在模板上显示拇指图像???