需要创建一个django相册

时间:2012-08-29 00:13:33

标签: python django django-models

我正在做一个需要用户照片上传和专辑创建的项目(比如facebook相册),其中一个用户可以在一个专辑中上传多张照片,可以上传多个专辑。经过搜索,我发现django imagestore应用程序非常方便。但不幸的是我没有找到任何示例的imagestore示例。我是django的新手。需要一些关于这个应用程序的示例教程。你能建议更好的方法来创建相册吗?

这是我创建相册的方法 -

def img_file_upload_path(instance, filename):
    """ creates unique-Path & filename for upload """
    ext = filename.split('.')[-1]
    filename = "%s%s.%s" % ('img', instance.pk, ext)

    return os.path.join(
        'images','eventpic','original',                                                      
        instance.event_id.channel_id.publisher.user.username, 
        instance.event_id.channel_id.channel_title, 
        instance.event_id.event_title, 
        filename
    )   

def formatted_img_file_upload_path(instance, filename):
    """ creates unique-Path & filename for upload """
    ext = filename.split('.')[-1]
    filename = "%s%s.%s" % ('img', instance.pk, ext)

    return os.path.join(
        'images','eventpic','formatted', 
        instance.event_id.channel_id.publisher.user.username, 
        instance.event_id.channel_id.channel_title, 
        instance.event_id.event_title,
        filename
    )    

def thumb_img_file_upload_path(instance, filename):
    """ creates unique-Path & filename for upload """
    ext = filename.split('.')[-1]
    filename = "%s%s.%s" % ('img', instance.pk, ext)

    return os.path.join(
        'images','eventpic','thumb', 
        instance.event_id.channel_id.publisher.user.username, 
        instance.event_id.channel_id.channel_title, 
        instance.event_id.event_title,
        filename
    )    

class Album(models.Model):
    album_id = models.AutoField(primary_key=True)
    event_id = models.ForeignKey(event_archive,db_column='event_id')
    name = models.CharField(max_length=128)
    summary = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)


class Photo(models.Model):  
    image_id            = models.AutoField(primary_key=True)
    album               = 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()
    original_image      = models.ImageField(upload_to=img_file_upload_path) 

    def save(self):
        if self.is_cover_photo:
            other_cover_photo = Photo.objects.filter(album=self.album).filter(is_cover_photo = True)
            for photo in other_cover_photo:
                photo.is_cover_photo = False
                photo.save()
        filename = self.img_file_upload_path()
        if not filename == '':
            img = Image.open(filename)
            if img.mode not in ("L", "RGB"):
                img = img.convert("RGB")

            img.resize((img.size[0], img.size[1] / 2),Image.ANTIALIAS)
            img.save(self.formatted_img_file_upload_path(),quality=90)
            img.thumbnail((150,150), Image.ANTIALIAS)
            img.save(self.thumb_img_file_upload_path(),quality=90)
        super(Photo, self).save()


    def delete(self):
        filename = self.img_file_upload_path()
        try:
            os.remove(self.formatted_img_file_upload_path())
            os.remove(self.thumb_img_file_upload_path())
        except:
            pass
        super(Photo, self).delete()

    def get_cover_photo(self):
        if self.photo_set.filter(is_cover_photo=True).count() > 0:
            return self.photo_set.filter(is_cover_photo=True)[0]
        elif self.photo_set.all().count() > 0:
            return self.photo_set.all()[0]
        else:
            return None

我无法解决的错误是

 filename = self.img_file_upload_path()

需要帮助来修复错误。您认为创建像photoalbum这样的Facebook的方法是否正确?或者我应该使用imagestore应用程序吗?在这里我想提一下我想保存格式化图像和拇指图像保持同时正在上传..需要您的专家评论和帮助。

1 个答案:

答案 0 :(得分:0)

即使没有看到回溯,我也很确定您的错误正在发生,因为您正在尝试调用Photo模型中不存在的方法:

def img_file_upload_path(instance, filename):
def formatted_img_file_upload_path(instance, filename):
def thumb_img_file_upload_path(instance, filename):

这些只是您定义的函数,并被指定为upload_to句柄,用于确定新保存的图像文件的路径上载路径。他们不会上课。为了让您能够手动调用它们,您必须执行以下操作:

filename = img_file_upload_path(self, 'original_name.jpg')

假设original_image设置正确,可能就像:

if self.original_image.name:
    filename = img_file_upload_path(self, self.original_image.name)