【发布时间】:2018-06-01 21:35:14
【问题描述】:
我有一个存储在 AWS S3 中的 ImageField(类似于 FileField)。在表单中,它具有显示图像文件路径的“当前”标签。我想修剪并只显示文件名。
参考Django : customizing FileField value while editing a model中的最新答案,我仍然无法正常工作。
它显示“当前”,文件路径名如下: https://imgur.com/a/xkUZi
form.py
class CustomClearableFileInput(ClearableFileInput):
def get_template_substitution_values(self, value):
"""
Return value-related substitutions.
"""
logging.debug("CustomClearableFileInput %s",value) <-- it never came here
return {
'initial': conditional_escape(path.basename(value.name)),
'initial_url': conditional_escape(value.url),
}
class CompanySettingEdit(forms.ModelForm):
display_companyname = forms.CharField(max_length=50, required=True)
company_logo = forms.ImageField(widget=CustomClearableFileInput)
class Meta:
model = Company
fields = ("display_companyname","company_logo")
模型.py
class Company(models.Model):
display_companyname = models.CharField(max_length=50)
company_logo = models.ImageField(upload_to=upload_to('company_logo/'), blank=True, null=True, storage=MediaStorage())
我怎么能有这样的东西:目前:filename.jpg
仅供参考 - ImageField / FileField,我都试过了,但没有任何区别。 我正在使用 Django==1.11.7
【问题讨论】:
-
你也可以分享你的
CustomClearableFileInput吗? -
抱歉忘记放了,和你的代码一样。不同之处在于 Meta 我有“company_logo”字段。这是否应该有所作为。我调试到 CustomClearableFileInput,它从来没有进入那个函数。调用时有什么问题吗?
-
你使用的是哪个 Django 版本!?
-
我正在使用 Django==1.11.7
标签: python django django-forms django-file-upload django-1.11