Uploaded images and documents belong to application data, not the disposable code release. Their storage and access rules need to survive deployments and protect private files.
IN THIS ARTICLE
What you need before you begin
Identify the configured default storage, media URL and any external object store. Classify which files may be public and which require a permission check before download.
01 Keep Django user uploads separate from static assets
Choose persistent storage outside release directories that are replaced or removed. Configure
MEDIA_ROOTandMEDIA_URLfor local storage, or use the selected storage backend.Keep uploads separate from
STATIC_ROOT. Make the upload path writable only by the required application account and prevent uploaded files from executing as server code.Validate file size and permitted content in the application. A filename extension or an image parser alone is not a complete policy for all potentially active content.
Serve private documents through an authorised delivery path. For untrusted public uploads, consider a separate content origin and suitable response headers rather than sharing application trust.
Back up upload data along with the database references that identify it. Rehearse restoring the matching data to a separate location.
02 Example: keep a customer document through redeployment
Upload a harmless private document using test account A and record its application identifier. Then deploy a new code release without replacing the persistent media store.
- Account A must still see the same document through the authorized download action.
- Test account B must not retrieve it by changing the document identifier or copying a private URL.
- A protected recovery copy must restore both the database reference and the corresponding file.
Running collectstatic prepares application assets such as stylesheets; it neither backs up this document nor repairs a missing media mount. If the database record survives but its file does not, inspect the media storage path, mount or backend credentials before restoring an older database over newer customer data.
Confirm the expected result
Upload a harmless file, redeploy the application and confirm it remains available. Verify a second user cannot retrieve a private document simply by guessing its URL.
Troubleshoot unexpected results
Broken media after a release can mean the new code points to an empty local directory. Check the persistent mount or storage credentials before restoring older database data.
A common question
Will collectstatic back up uploaded files?
No. It prepares static application assets. Media backups require their own storage-aware process and should be coordinated with the database backup.