In Django the following settings
are configured:
MEDIA_URL="https://abcdefg.cloudfront.net/media/"
STATIC_URL="https://abcdefg.cloudfront.net/static/"
In web browsers the URL for the static files is correct.
The URL for media files however is incorrect with https://https:
leading to 404
's:
https://https://abcdefg.cloudfront.net/media/
When I print(f"MEDIA_URL: {MEDIA_URL}")
, from the settings the URL is correct.
I use {% get_media_prefix %}
and {% get_static_prefix %}
for my static and media files. For example:
<img src="{% get_media_prefix %}image.jpg" alt="Image">
This alternative does not work:
MEDIA_URL="abcdefg.cloudfront.net/media/"
The result observed in the browser is https://example.com/abcdefg.cloudfront.net/media/
EDIT: Am using Django-CMS. Only the URLs for Django-CMS seem to show the above behavior.
What is the issue here?
Solution:
Triggered by this, I figured out based on the django-storages
documentation, that specifically for Django-CMS, when using CloudFront
I had to add AWS_S3_CUSTOM_DOMAIN
to my settings. The format is different, however:
STATIC_URL="https://abcdefg.cloudfront.net/static/"
MEDIA_URL="https://abcdefg.cloudfront.net/media/"
AWS_S3_CUSTOM_DOMAIN="abcdefg.cloudfront.net"