Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Django repopulate file fields on form error?

Currently I have something like:

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})

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?

like image 993
Jason Christa Avatar asked Jan 22 '23 11:01

Jason Christa


1 Answers

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/

like image 60
Mark Lavin Avatar answered Feb 04 '23 04:02

Mark Lavin