这是我的问题的后续:ImageField / FileField Django 表单当前无法修剪文件名的路径 https://stackoverflow.com/questions/47887158/imagefield-filefield-django-form-currently-unable-to-trim-the-path-to-filename
在我的 Django 应用程序中,有一个图像字段上传到 S3
修剪图像文件路径名后,由于 url 被修剪,因此无法访问图像。如何修剪显示但不修剪路径?
我设法修剪显示文件名的显示,如下所示
class CustomClearableFileInput(ClearableFileInput):
def get_context(self, name, value, attrs):
logging.debug("%s",name)
logging.debug("%s",value)
value.name = path.basename(value.name)
context = super().get_context(name, value, attrs)
return context
class CompanySettingEdit(forms.ModelForm):
company_logo = forms.ImageField(widget=CustomClearableFileInput)
这是输出:
https://imgur.com/a/M42Mz <-- display correct
https://bucketname.s3.amazonaws.com/media/certiport_logo.png <-- invalid url
如果我不修剪它:
class CustomClearableFileInput(ClearableFileInput):
def get_context(self, name, value, attrs):
logging.debug("%s",name)
logging.debug("%s",value)
# value.name = path.basename(value.name) <-- remove this
context = super().get_context(name, value, attrs)
return context
class CompanySettingEdit(forms.ModelForm):
company_logo = forms.ImageField(widget=CustomClearableFileInput)
这是输出:
https://imgur.com/a/rGi8f <-- display incorrect
https://bucketname.s3.amazonaws.com/media/company_logo/15/certiport_logo.png <--valid url
我的目标是:
display: certiport_logo.png
url: https://bucketname.s3.amazonaws.com/media/company_logo/15/certiport_logo.png
我怎样才能做到这一点?