2012-08-29 48 views
-2

我正在做一個項目,需要用戶照片上傳和創建相冊(如Facebook相冊),其中一個用戶可以上傳一個相冊中的多張照片,可以上傳多個相冊。如此搜索後,我發現django imagestore應用程序非常方便。但不幸的是我沒有找到任何示例礦石教程的imagestore.I在django非常新。需要關於這個應用程序的一些示例教程..你能否提出更好的方式來創建一個相冊?需要創建一個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() 

需要幫助修復error.Do你想辦法可以創建一個像photoalbum一樣的facebook嗎?或者我應該使用imagestore應用程序嗎?在這裏我想提到的是,我想保存格式化的圖像和拇指圖像保持上傳..需要您的專家審查和幫助。

+1

安置自己的錯誤回溯。 – jdi

+0

雖然我上傳照片submit..Exception類型在此之後eroor發生:\t AttributeError的 異常值:\t 「照片」對象有沒有屬性「img_file_upload_path」 – oxvoxic

回答

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) 
+0

好奇,想知道我的做法是確定以創建一個基於用戶的照片專輯??你有任何提示??我找到了imagestore應用程序,但無法找到任何有關此教程?我應該使用django imagestore應用程序? – oxvoxic

+1

我不確定最好的方法是什麼,因爲我還沒有創建一個。我可能只會研究已經存在的選項,並檢查它們的開發活動有多活躍。重新發明車輪毫無意義。如果維護良好的django應用程序存在,爲什麼不使用它? – jdi