Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ImageField/FileField Django表单当前无法修剪文件名的路径_Python_Django_Django Forms_Django File Upload_Django 1.11 - Fatal编程技术网

Python ImageField/FileField Django表单当前无法修剪文件名的路径

Python ImageField/FileField Django表单当前无法修剪文件名的路径,python,django,django-forms,django-file-upload,django-1.11,Python,Django,Django Forms,Django File Upload,Django 1.11,我有一个存储在AWS S3中的ImageField(类似于FileField)。在表单中,它具有显示图像文件路径的“当前”标签。我想修剪,只是显示文件名 参考年的最新答案,我仍然无法使它起作用 它显示“当前”,文件路径名如下: form.py class CustomClearableFileInput(ClearableFileInput): def get_template_substitution_values(self, value): """

我有一个存储在AWS S3中的ImageField(类似于FileField)。在表单中,它具有显示图像文件路径的“当前”标签。我想修剪,只是显示文件名

参考年的最新答案,我仍然无法使它起作用

它显示“当前”,文件路径名如下:

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")
我怎么能有这样的东西:current:filename.jpg

仅供参考-ImageField/FileField,我尝试了这两种方法,但没有任何区别。
在Django 1.11.x
get\u template\u substitution\u values
中,Im使用Django==1.11.7

CustomClearableFileInput
的新实现可以如下所示:

class CustomClearableFileInput(ClearableFileInput):
    def get_context(self, name, value, attrs):
        value.name = path.basename(value.name)
        context = super().get_context(name, value, attrs)       
        return context

您是否也可以共享您的
CustomClearableFileInput
?很抱歉,忘了放上它,它与您的代码相同。不同之处在于,我有“公司标志”字段。这应该会有所不同。我调试到CustomClearableFileInput,但它从未进入该函数。调用时是否有问题?您使用的是哪个Django版本!?我使用Django==1.11.7它可以工作,但有一个问题,这个链接现在不正确。它指向当前页面(),而它应该指向不起作用的图像url(类似这样的内容),这与最初的问题相同。它没有修剪。我让它使用value.name=path.basename(value.name)。编辑答案。非常感谢,更改对象的内部状态很危险,但这似乎是唯一的解决方法,否则您将不得不覆盖小部件的HTML模板。干得好+1:@DhiaTN你能检查一下我接下来的问题吗,不知怎么的,它没有完成解决方案。
class CustomClearableFileInput(ClearableFileInput):
    def get_context(self, name, value, attrs):
        value.name = path.basename(value.name)
        context = super().get_context(name, value, attrs)       
        return context