Skip to content

Knowledgebase

Keeping Django user uploads separate from static assets

development

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.

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

  1. Choose persistent storage outside release directories that are replaced or removed. Configure MEDIA_ROOT and MEDIA_URL for local storage, or use the selected storage backend.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  1. Account A must still see the same document through the authorized download action.
  2. Test account B must not retrieve it by changing the document identifier or copying a private URL.
  3. 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.

Was this answer helpful?

Need a little more help?

Our support team can help you find the next step.

Contact support