モデルでサムネイル画像を作成しました。この関数は、サムネイル画像の動的パス 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))
ここで立ち往生しました..1つのアルバムの下にあるすべての画像を表示する必要があります..元の画像を取得しましたが、サムネイルとフォーマットされたサイズを呼び出すことができませんでした.Photoクラスからの関数で画像パスを返し、2つの引数を受け取るためです. テンプレートに親指の画像を表示するにはどうすればよいですか???