0

モデルでサムネイル画像を作成しました。この関数は、サムネイル画像の動的パス 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つの引数を受け取るためです. テンプレートに親指の画像を表示するにはどうすればよいですか???

4

1 に答える 1

0

ファイサル、

次のようにして、フォーマットされた画像の取得を試すことができます。

formatted_photo = [p.get_formatted_image(p.photo) for p in e_piclist]

しかし、より良いアプローチは、get_formatted_image メソッドをリファクタリングして self.photo を使用し、それをテンプレートで直接使用することです。

そして、django-imagekit を使えばさらに良いでしょう。

特にインデントがすべてであるpythonを貼り付けるときは、質問するときにコードをより適切にフォーマットすることをお勧めします。あなたの質問を誤解した場合は申し訳ありません。

于 2012-09-02T15:34:13.483 に答える