Django - forms.FileField() initial value

后端 未结 2 548
鱼传尺愫
鱼传尺愫 2020-12-06 10:55

Is it possible to give initial value for forms.FileField() dynamically?

相关标签:
2条回答
  • 2020-12-06 11:21

    I'm not sure, if this is what you want, but…

    Obviously you cannot set initial data for a file input (this would imply that you send a file to the user). Django has a ClearableFileInput widget though, and this widget pretends to show initial data: it displays a link to the uploaded file if the link is present.

    This is implemented through checking if the initial object (the object passed to form constructor in files dictionary) has the url attribute. Once you set this attribute, the widget will display the "Currently link" line.

    By the way, if you got the file object using a File Storage, then you can get the link to that file using storage's url() method.

    0 讨论(0)
  • 2020-12-06 11:30

    By security standards, <input type="file"> can't have a default value and it can't be manipulated.

    The reason is, that a hacker can set the default value of the file input to any important file on your system (containing passwords, certificates and so on), hide this field using CSS and you will never know that you have uploaded something to hackers host.

    0 讨论(0)
提交回复
热议问题