【问题标题】:Need to create a django photo album需要创建一个django相册
【发布时间】:2012-08-23 13:14:30
【问题描述】:

我正在做一个需要用户照片上传和相册创建的项目(如 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 应用程序吗?在这里我想提一下,我想保存格式化的图像和拇指图像,同时保持上传..需要您的专家审查和帮助。

【问题讨论】:

  • 当我上传照片时,提交后出现此错误。异常类型:AttributeError 异常值:“照片”对象没有属性“img_file_upload_path”

标签: python django django-models


【解决方案1】:

即使没有看到回溯,我也很确定您的错误正在发生,因为您正在尝试调用 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)

【讨论】:

  • 想知道我的方法是否可以创建基于用户的相册?您有什么建议吗??我找到了 imagestore 应用程序,但找不到任何关于此的教程?我应该使用 django imagestore 应用??
  • 我不确定最好的方法是什么,因为我还没有创建一个。我可能只是研究已经存在哪些选项,并检查它们的开发有多活跃。重新发明*是没有意义的。如果存在维护良好的 django 应用程序,为什么不使用它?