Django 将图像名称与图像路径分开
视图.pyDjango 将图像名称与图像路径分开,django,python-2.7,Django,Python 2.7,视图.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('/')]
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()
class profiles(models.Model):
image = models.ImageField(blank=True,upload_to='%Y/%m/%d')
user = models.OneToOneField(User)
型号.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()
class profiles(models.Model):
image = models.ImageField(blank=True,upload_to='%Y/%m/%d')
user = models.OneToOneField(User)
2012/09/08/4f31063d985c97b64e930917b456083c.jpg
我想从这个链接中分离图像名称 使用os.path.basename(path)
:返回作为字符串传递的路径名路径的基本名称
>> os.path.basename('2012/09/08/4f31063d985c97b64e930917b456083c.jpg')
4f31063d985c97b64e930917b456083c.jpg
阅读更多信息“ImageFieldFile”对象没有属性“rfind我在执行此操作时遇到此错误。**image=os.path.basename(图像)**。
basename
的参数必须是字符串。谢谢。@warren weckesserNow我将图像转换为字符串“str(图像)”,并获得了结果。现在我将图像转换为字符串“str(图像)””他说。