Django 会在表单错误时重新填充文件字段吗? [英] Does Django repopulate file fields on form error?

查看:17
本文介绍了Django 会在表单错误时重新填充文件字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有类似的东西:

def my_view(request)
    if request.method == 'POST':
        form = MyForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            redirect()
    else:
        form = MyForm()
    return render_to_response('form.html', {'form': form})

在表单验证错误时,所有与 request.POST 关联的字段都会重新填充,但带有 request.FILES 的字段为空.这是已知的 Django 限制还是我可以对重新填充的文件字段做些什么?

On a form validation error, all the fields associated with request.POST are repopulated but the fields with request.FILES are empty. Is this a known Django limitation or is there something I can do to my the file fields repopulate?

推荐答案

不,这不是 Django 问题,而是浏览器问题.文件字段不能填充初始值,否则在用户不知情的情况下使用恶意表单从用户机器上传文件将是微不足道的.在 django-users 邮件列表上有几个关于此的主题:
http://groups.google.com/group/django-users/浏览线程/线程/14922dca454e3782/
http://groups.google.com/group/django-users/browse_thread/thread/f9fb21ddb4039b33/

No and this isn't as much a Django issue as a browser issue. File fields cannot be populated with an initial value otherwise it would be trivial to have a malicious form to upload files from the user's machine without their knowledge. There have been a couple threads about this on the django-users mailing list:
http://groups.google.com/group/django-users/browse_thread/thread/14922dca454e3782/
http://groups.google.com/group/django-users/browse_thread/thread/f9fb21ddb4039b33/

这篇关于Django 会在表单错误时重新填充文件字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆