Django 表单文件字段因表单错误而消失
问题是,我有一个包含文件字段的 Django 表单,即:
photo = forms.FileField(help_text="Please attach a photo", required=False)
如果表单验证,则文件字段被限制并正确保存。 问题是当用户填写所有表单但未验证时:所选文件的路径消失。
因此,如果用户没有意识到这一点,他/她会修复其他字段错误并再次提交 - 这次没有照片。
为了以防万一,表单在视图中创建如下:
ProfileForm(request.POST or None, request.FILES or None)
HTML 为:
<div id="uniform-id_photo" class="uploader">
<input id="id_photo" class="clearablefileinput" type="file" name="photo" size="19" style="opacity: 0;">
<span class="filename" style="-moz-user-select: none;">No file selected</span>
<span class="action" style="-moz-user-select: none;">Choose File</span>
</div>
以前有人遇到过同样的问题吗?对解决方案有什么想法吗? :)
谢谢!
Here is the problem, I have a Django Form containing a File field, namely:
photo = forms.FileField(help_text="Please attach a photo", required=False)
If the form validates, the File field is bounded and saved correctly. The problem is when the user fills all the form and it doesn't validate: the path of the selected file disappear.
So, if the user doesn't realize about this, he/she fix the other fields errors and submit again - with no photo this time.
Just in case, the form is created in the view like this:
ProfileForm(request.POST or None, request.FILES or None)
and the HTML is:
<div id="uniform-id_photo" class="uploader">
<input id="id_photo" class="clearablefileinput" type="file" name="photo" size="19" style="opacity: 0;">
<span class="filename" style="-moz-user-select: none;">No file selected</span>
<span class="action" style="-moz-user-select: none;">Choose File</span>
</div>
Has anyone had the same problem before? Any thoughts towards a solution? :)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这是浏览器强加的一个问题(实际上是一个安全功能),并且无法解决。浏览器不允许您为文件输入指定初始值,并且您无法解决该问题。
原因是,如果一个网站可以做到这一点,它将打开一个向量,允许任何网站通过猜测文件路径来窃取您计算机上的任何文件 - 他们可能只是在后台运行一个脚本,试图发布有趣的内容文件返回到服务器。
唯一的解决方法是实际将上传的文件保存在服务器上,无论表单是否验证,然后当您将表单和错误呈现给用户时,表明您已收到文件并且他们应该只填写该字段来替换它。
Unfortunately, this is a problem (really a security feature) imposed by browsers, and is not solvable, as such. Browsers will not let you specify an initial value for file inputs, and there's nothing that you will be able to do to work around that.
The reason is that if a website could do this, it would open up a vector that would allow any website to steal any file on your computer by guessing file paths--they could just have a script running in the background that tried to post interesting files back to the server.
The only workaround is to actually save the uploaded file on the server regardless of whether the form validates, and then when you render the form and errors back to the user, indicate that you have received a file and that they should only fill out that field to replace it.
我写了一些解决方案:
然后在你的表格中你必须写:
它对我有用。我想你也不会有问题:)
I write some solution:
And then in your form you must write:
It works for me. I think you will have no problem too :)