CDN for Django

Point a CDN hostname at the output of collectstatic. Treat public media uploads separately from private files that require an application permission check.

Example cache policy
Public, cacheable resource
/static/admin/css/base.6d9c.css
Bypass shared cache
/admin/login/

Distribute collected static files

Point STATIC_URL and MEDIA_URL at Cloud CDN. Cache anonymous pages where safe. Bypass admin, authenticated views and POST.

Deliver collectstatic assets at the edge

Fingerprinted static files can stay cached when collectstatic publishes a new version.

MEDIA_URL worldwide

Uploads are cached and can be resized at delivery time.

Admin stays on Django

django-admin and session views are not cached.

Configuration

Cloud CDN overview
  1. Set STATIC_URL to the CDN hostname

    After collectstatic lands on an origin Cloud CDN can pull.

  2. Do the same for public media

    Map MEDIA_URL for publicly readable uploads.

  3. Cache anonymous views optionally

    Marketing or docs views can be cached with purge.

  4. Keep CSRF and admin dynamic

    Never cache POST or authenticated HTML.

Operational outcomes

  • Django staticfiles are global
  • Public media is cached at the edge
  • Admin and CSRF flows remain on origin
  • Workers handle views instead of CSS

Frequently asked questions

Does this replace WhiteNoise?

WhiteNoise can still serve as origin. Cloud CDN caches that result worldwide and offloads the app cluster.

How should ManifestStaticFilesStorage work?

Keep hashed filenames. Long-cache those files and purge unhashed HTML on deploy.

Can I cache Django REST framework GET?

Public, identical GET responses can be cached. Authenticated APIs must bypass cache.

What about django-storages?

Use the storage hostname as origin or sit Cloud CDN in front of it for the public URL.