Axi*_*xil 10 python django amazon-s3 imagefield
这是我的问题的后续跟进:ImageField/FileField Django表单目前无法修剪文件名的路径
在我的Django应用程序中,有一个上传到S3的图像字段修剪图像文件路径名后,由于修剪了网址,因此无法访问该图像.我如何修剪显示但不修剪路径?
我设法修剪显示文件名的显示
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)
Run Code Online (Sandbox Code Playgroud)
这是输出:
https://imgur.com/a/M42Mz <-- display correct
https://bucketname.s3.amazonaws.com/media/certiport_logo.png <-- invalid url
Run Code Online (Sandbox Code Playgroud)
如果我不修剪它:
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)
Run Code Online (Sandbox Code Playgroud)
这是输出:
https://imgur.com/a/rGi8f <-- display incorrect
https://bucketname.s3.amazonaws.com/media/company_logo/15/certiport_logo.png <--valid url
Run Code Online (Sandbox Code Playgroud)
我的目标是:
display: certiport_logo.png
url: https://bucketname.s3.amazonaws.com/media/company_logo/15/certiport_logo.png
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
Gor*_*ran -1
对于你的情况,我建议JSONField而不是 ImageField。这样你就可以拥有:
obj.my_image_field = {'display': 'certiport_logo.png', 'url': 'your_S3_url'}
obj.save()
Run Code Online (Sandbox Code Playgroud)
类示例:
class MyModel(models.Model):
my_image_field = JSONField()
def get_image_display(self):
return self.my_image_field.get('display', None)
def get_image_url(self):
return self.my_image_field.get('url', None)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
317 次 |
| 最近记录: |