Django File Upload Handler Errors

瘦欲@ 提交于 2019-12-06 07:34:25

I've found a reason for why I couldn't get it working and a solution as well.

It would appear that Django uses lazy evaluation for request.FILES to determine when the upload handler is called. Therefore, the upload handler is only evoked when and if you attempt to access request.FILES. Additionally, the request object I am using (WSGIRequest in my case) has made GET and POST immutable dictionaries, so we can't pass information through there. However, META is still available to add information to.

My combined solution has the line "request.FILES" in the view that handles uploads, which forces the upload handler to begin. When the error is captured in new_files, I set self.request.META['error'] to the error message and raise StopUpload, which pushes us back into the view without a file. Finally, I check for request.META['error'] in the view and display that message when there is a problem.

I hope this helps!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!