从图像路径中分离图像名称

时间:2014-01-25 05:13:32

标签: django python-2.7

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我想将图片名称与此链接分开。

1 个答案:

答案 0 :(得分:1)

使用os.path.basename(path):返回以字符串形式传递的路径名路径的基本名称。

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

详细了解here.