2014-01-25 112 views
0

views.py獨立的圖像名稱

def index(request): 
    profile = profiles.objects.all() 
    for person in persons: 
     images = profile.image 
     image = 'uploads/no-img.jpg' 
     if images: 
      [x.strip() for x in images.split('/')] 
      image = images[-1:] 
     p = image_name(image = image,activated = 1) 
     p.save() 

models.py

class profiles(models.Model): 
    image = models.ImageField(blank=True,upload_to='%Y/%m/%d') 
    user = models.OneToOneField(User) 

2012/09/08/4f31063d985c97b64e930917b456083c.jpg我想圖像名稱從這個鏈接分開。

+2

http://docs.python.org/2/library/os.path.html#os.path.basename –

+0

'ImageFieldFile' 對象有沒有屬性「RFIND 我得到這個錯誤,當我這樣做。** image = os.path.basename(images)**。 – Thameem

+1

「basename」的參數必須是字符串。 –

回答

1

使用os.path.basename(path):返回作爲字符串傳遞的路徑名路徑的基名。

>> os.path.basename('2012/09/08/4f31063d985c97b64e930917b456083c.jpg') 
    4f31063d985c97b64e930917b456083c.jpg 

瞭解更多關於它here.

+0

謝謝。@ santhosh ghimire – Thameem

+0

現在我將圖像轉換爲字符串'str(image)'並得到結果。 – Thameem