How do you handle file uploads in Django?

Instruction: Describe the process and considerations for handling file uploads in a Django application.

Context: This question tests the candidate's knowledge of handling file uploads, including security and performance considerations.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

An HTML upload form uses POST with enctype="multipart/form-data" and the required CSRF protection. Bind both request.POST and request.FILES to the form. A ModelForm with a FileField can save the file through the configured storage and store its name in the model.

Authorize the uploader and target object first, then validate the application's file contract. The browser's accept attribute, client filename and declared MIME type are not trustworthy security checks. Extension, signature and parser checks provide different evidence; none alone proves a file harmless...

Related Questions