-3

我正在做一个需要用户上传照片和创建相册的项目(如 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()

需要帮助解决错误。您认为创建像相册这样的 facebook 的方法可以吗?或者我应该使用 imagestore 应用程序吗?在这里我想提一下,我想在上传时保存格式化的图像和拇指图像。需要您的专家审查和帮助。

4

1 回答 1

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)
于 2012-08-29T00:42:11.657 回答