什么是用于显示缩略图图像的模板标签

时间:2012-09-01 18:53:07

标签: python django django-models django-templates django-views

我在模型中创建了缩略图图像,此函数返回缩略图图像的动态路径网址。现在我将如何在我的模板中使用它?我是否必须写一些观点功能? 这是我的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))  

我插在这里..我需要在一张专辑下显示所有图像..我得到了原始图像,但无法调用拇指和格式化大小,因为它们返回Photo类中的函数中的图像路径并接收2个论点。我现在应该怎么做才能在模板上显示拇指图像

1 个答案:

答案 0 :(得分:0)

费萨尔,

您可以通过执行以下操作来尝试对格式化图像进行格式化:

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

但更好的方法是重构你的get_formatted_image方法以使用self.photo并直接在模板中使用它。

如果使用django-imagekit,它会更好。

我最好在询问时更好地格式化代码,特别是当粘贴python时,缩进就是一切。 对不起,如果我误解了你的问题。