Django 验证错误后重新提交 ImageField 中的图像
我的管理界面中有一个带有 ImageField 的表单。一切都很好,除非其他字段引发验证错误。在这些情况下,表单将返回给用户进行更正,但已加载的图像文件将从表单中清除。
知道如何以某种方式将已提交的图像重新加载到表单中以便保存图像吗?
谢谢!
一些有趣的代码
class DealForm(forms.ModelForm):
image = forms.ImageField(required=False,widget=AdminImageWidget)
def clean():
data = self.cleaned_data
date_start = data.get('date_start')
date_end = data.get('date_end')
(... several other validations ...)
return data
:
class AdminImageWidget(forms.FileInput):
def __init__(self, attrs={}):
super(AdminImageWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
output = []
if value and hasattr(value, "url"):
output.append(('<a target="_blank" href="%s">'
'<img src="%s" /></a> '
% (value.url, value.url_200x150)))
output.append(super(AdminImageWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
I have, in my admin interface, a form with a ImageField. Everything works great except when some other field raises a validation error. In those cases, the form returns to the user for correction, but the already loaded image file is cleared from the form.
Any idea in how to, somehow, reload the already submitted image to the form in order to allow the image beeing saved?
Thanks!
SOme interesting bits of code:
class DealForm(forms.ModelForm):
image = forms.ImageField(required=False,widget=AdminImageWidget)
def clean():
data = self.cleaned_data
date_start = data.get('date_start')
date_end = data.get('date_end')
(... several other validations ...)
return data
.
class AdminImageWidget(forms.FileInput):
def __init__(self, attrs={}):
super(AdminImageWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
output = []
if value and hasattr(value, "url"):
output.append(('<a target="_blank" href="%s">'
'<img src="%s" /></a> '
% (value.url, value.url_200x150)))
output.append(super(AdminImageWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTML
字段无法预先填充数据。因此,如果在另一个字段上验证失败,您将必须重新选择图像。
这是一种 HTML/浏览器安全措施。就像,没有办法解决它!
想象一下,如果一个网站可以将
C:\Windows\something_important
注入到页面一角的表单中会怎样?如果这是关键功能,您可以...
HTML
<input type="file">
fields cannot be prepopulated with data. Therefore, if validation fails on another field, you will have to re-select the image.It's a HTML / browser security measure. As in, no way around it!
Imagine if a site could inject
C:\Windows\something_important
into a form in the corner of the page?If this is critical functionality, you could...
这是 django 应用程序,称为 django-file-resubmit。它正好解决了这个问题。
https://github.com/un1t/django-file-resubmit
Here is django app, called django-file-resubmit. It solves exactly this problem.
https://github.com/un1t/django-file-resubmit
如果验证失败,您需要发送图像文件作为响应的一部分,然后 JavaScript 应从那里继续。
在模板中添加所需的 JavaScript 代码:
dataURItoBlob 函数:
If validation fails you need to send the image file as part of the response then JavaScript should continue from there.
in your template add the required JavaScript code:
dataURItoBlob function: