Opened 9 years ago

Closed 9 years ago

#26049 closed Uncategorized (worksforme)

Request _upload_handlers is immutable?

Reported by: Alexander Whillas Owned by: nobody
Component: File uploads/storage Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The documentation states (https://docs.djangoproject.com/en/1.8/topics/http/file-uploads/#id1)
that you can change the upload hander at the beginning of a view by inserting an instance of your upload handler at the beginning of, what turns out to be, an ImmutableList

request.upload_handlers.insert(0, ProgressBarUploadHandler())

which is because its... immutable. It complain()'s with a weird error:

You cannot alter upload handlers after the upload has been processed.

which is triggered from django/utils/datastructures.py line 500. If one tries the other approach suggested in the documentation

request.upload_handlers = [ProgressBarUploadHandler()]

one gets the same error again only triggered from the setter function in django/http/request.py line 215. So hasattr(self, '_files') is true even though the error reports No FILES data for FILES. The solution I came up with that seems to actually use my custom upload hander is:

request._upload_handlers = ['YoMamasPoject.YoMamasApp.handlers.ProgressBarUploadHandler']

Which is obviously ugly.
I guess nobody has written a test to test this feature? Is the request.upload_handlers list supposed to be immutable, in which case change the docs or make it mutable? Also ether the setter function for it is wrong or the error report about FILES is wrong, one of them needs to change.

Change History (2)

comment:1 by Tim Graham, 9 years ago

The tests are here.

It sounds like you're running into the issue described in the note following the section you described:

You can only modify upload handlers before accessing request.POST or request.FILES – it doesn’t make sense to change upload handlers after upload handling has already started. If you try to modify request.upload_handlers after reading from request.POST or request.FILES Django will throw an error.

If your view isn't accessing those vlaues, then maybe a middleware is?

comment:2 by Tim Graham, 9 years ago

Resolution: worksforme
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top