Programming Tech

Django File Uploads Fail

While trying to add a simple “Attachments” feature to one of my applications I ran across an issue where a record with file could be added from the Django Admin, but would fail with a validation error when used in my forms.

The error came down to my missing something from in the form instantiation from the POST data.

def handle_files(request):
  if request.method == 'POST':
    form = MyForm(request.POST, request.FILES)
    if form.is_valid():    

I was missing the additional parameter for the FILES from the request object. I ran across so many articles that didn’t show THAT, or I was just missing it, that I spent far too long on trying to solve this simple error.

Update: This is covered in the Django documentation, but somehow I managed to over look it every single time I read it.

Dennis Bottaro, Harried Programmer in a Hurry

Hopefully this saves someone the time it took me to resolve this issue.