python - Django : customizing FileField value while editing a model

标签 python django file-upload django-forms django-file-upload

我有一个模型,带有 FileField。当我在 View 中编辑此模型时,我想更改显示在 View 表单中的 FileField 的“当前”值。让我解释一下。

模型.py:

class DemoVar_model(models.Model):
    ...
    Welcome_sound=models.FileField(upload_to='files/%Y/%m/%d')

表单.py:

class DemoVar_addform(ModelForm):
    ...
    class Meta:
        model = DemoVar_model        

views.py:

soundform = DemoVar_addform(instance=ivrobj)
....
return render_to_response(template,{'soundform':soundform}, ....)

现在我想在我的 View 中编辑这个模型。当我在浏览器中查看时,我看到表单显示为

Welcome sound: Currently: welcome_files/2011/04/27/15_35_58_ojCompany.wav.mp3 
Change : <Choose File button>

我想更改此“当前”值,它描述文件在我的服务器上存在时的整个路径。我想将此字符串修剪为没有路径的文件名。我该如何实现?

最佳答案

您需要覆盖当前使用的 ClearableFileInput,以更改其显示方式。

这是继承自默认 ClearableFileInput 的新 ShortNameFileInput 的代码,只是在第 19 行进行了更改以仅显示文件名:

from django.forms.widgets import ClearableFileInput
import os
# missing imports
from django.utils.safestring import mark_safe
from cgi import escape
from django.utils.encoding import force_unicode

class ShortNameClarableFileInput(ClearableFileInput):
    def render(self, name, value, attrs=None):
        substitutions = {
            'initial_text': self.initial_text,
            'input_text': self.input_text,
            'clear_template': '',
            'clear_checkbox_label': self.clear_checkbox_label,
        }
        template = u'%(input)s'
        substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)

        if value and hasattr(value, "url"):
            template = self.template_with_initial
            substitutions['initial'] = (u'<a href="%s">%s</a>'
                                        % (escape(value.url),
                                           escape(force_unicode(os.path.basename(value.url))))) # I just changed this line
            if not self.is_required:
                checkbox_name = self.clear_checkbox_name(name)
                checkbox_id = self.clear_checkbox_id(checkbox_name)
                substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name)
                substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id)
                substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
                substitutions['clear_template'] = self.template_with_clear % substitutions

        return mark_safe(template % substitutions)

要在表单中使用它,您必须手动设置要使用的小部件:

class DemoVar_addform(ModelForm):
    ...
    class Meta:
        model = DemoVar_model
        widgets = {
            'Welcome_sound': ShortNameClarableFileInput,
        }                    

这应该可以解决问题。

关于python - Django : customizing FileField value while editing a model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5810774/

相关文章:

java - 在android中使用ftp4j连接到FTP服务器时出错

php - 使用 uniqid() 上传时的唯一文件名

perl - 我应该清理我的 Perl CGI HTML 表单中的文件上传输入字段吗?

python - 预先分类的经过训练的 Twitter 评论以进行分类

python - 使用Python查询BigQuery,这个Python表是如何工作的?

python - 如何在不使用任何包的情况下从 django url 加密 pk?

django - 构建已完成,但查看文档链接已损坏

python - ClassName(object) 中的 super(ClassName, self) 有什么作用? (在 django 文档中使用)

Python - 使用带通配符的 str.replace

python - 将数据从Python列表和字典写入CSV