【发布时间】: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