Integrate a CDN resource with Django CMS
Django CMS can deliver static files and uploaded media through a Orange's CDN resource by using the CDN domain in its asset URL settings.
Back up site files and the database before making changes. These steps assume standard Django STATIC_URL, STATIC_ROOT, MEDIA_URL, and MEDIA_ROOT settings without a custom storage backend or asset pipeline.
Create a CDN resource before starting the integration. Configure its custom domain with a valid CNAME record and an active SSL certificate.
-
If the project uses a virtual environment, activate it with the command appropriate to the environment. For an environment named
envon Linux or macOS, run:source env/bin/activate -
From the directory that contains
manage.py, collect static files intoSTATIC_ROOT:python manage.py collectstaticThe command copies static files into
STATIC_ROOT; it doesn't publish that directory or process uploaded media. -
Configure the origin web server or storage service to expose both Django asset directories:
- Serve
STATIC_ROOTunder/static/. - Serve
MEDIA_ROOTunder/media/.
The Django deployment process requires a web server or storage service to serve the collected static directory. Uploaded media remains separate from
collectstaticand must be available fromMEDIA_ROOT. - Serve
-
Before changing the public asset URLs, request a known static file and uploaded media file directly from the origin. Use the origin protocol and hostname configured for the CDN resource:
https://origin.example.com/static/test.css https://origin.example.com/media/example.jpgContinue only after both origin URLs return the expected files.
-
In the CDB Customer Portal, find the custom domain on the CDN resources page. Then set
STATIC_URLandMEDIA_URLinsettings.pyto that domain:STATIC_URL = 'https://cdn.site.com/static/' MEDIA_URL = 'https://cdn.site.com/media/' -
In templates, reference static files with the
{% static %}template tag:{% load static %} <link rel="stylesheet" type="text/css" href="{% static 'test.css' %}"> -
Restart or redeploy the Django application with the method used in the production environment so that it reloads the updated
settings.py. -
Confirm the integration by opening the browser's developer tools (press F12), selecting the Network tab, and refreshing the page — static and media files should now load from the custom domain instead of the origin domain.