Django static url setting is not working in production
I have uploaded stsatic files to Cloudflare R2 with python manage.py collectstatic
command, and checked files are there.
Also, allowed public access for the R2 bucket.
I have these in my settings.py
file for static related:
STATIC_HOST = env("STATIC_HOST")
STATIC_URL = STATIC_HOST + "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
STATICFILES_STORAGE = (
"django.contrib.staticfiles.storage.StaticFilesStorage"
if DEBUG
else "myproject.backends.storages.StaticStorage"
)
...and this is my .env
file:
DEBUG=True
AWS_ACCESS_KEY_ID=my_r2_access_key_id
AWS_SECRET_ACCESS_KEY=my_r2_secret_access_key
AWS_STORAGE_BUCKET_NAME=my_r2_bucket_name
# Endpoint for uploads
AWS_S3_ENDPOINT_URL=https://my_account_id.r2.cloudflarestorage.com
# Public access url
STATIC_HOST=https://pub-a1b2c3d4e5f6.r2.dev
When I runserver
with DEBUG=False
and go to localhost:8000/admin
it does not load CSS properly, and static file requests are all 4xx
and not using provided STATIC_URL
but AWS_S3_ENDPOINT_URL
.
When I runserver
with DEBUG=True
it loads static files properly from provided STATIC_URL
setting.
I want to load static files from R2 bucket in production / DEBUG=False
and use local static files with DEBUG=True
.
Please help me what I am doing wrong?