Управление статическими файлами отдельно для загрузки медиафайлов и отдельно для React Build

У меня есть небольшая проблема с моей текущей конфигурацией статических файлов.

В настоящее время Settings.py

#...
GOOGLE_APPLICATION_CREDENTIALS = BASE_DIR / 'Cloud_Storage_Secrets/model-calling-323507-96957393f399.json'
STATIC_URL = '/static/'
MEDIA_URL = '/images/'

STATICFILES_DIRS = [
    BASE_DIR / 'static',
    BASE_DIR / 'build/static' #React Frontend Files
]

MEDIA_ROOT = BASE_DIR / 'static/images'
STATIC_ROOT = BASE_DIR / 'staticfiles'

GS_BUCKET_NAME = 'GS_BUCKET_NAME'

STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    GOOGLE_APPLICATION_CREDENTIALS
)
#...

Now the problem is that React frontend Build is also considered as static files when Django serves these files. Locally it works fine after creating a build from the npm run build command manually. But when I push this in production with two separate engines One Node.js for React and the other is Python for Django app on Heroku, then Django tries to get Build files from GS_BUCKET as configured for Media files and static files. So frontend build files are not picked by Django.

So I'm trying to make two configurations. Google Cloud Storage for just media files but Static Files should work as default bypassing Google Cloud Storage.

So How to make it possible. Please suggest to me any method to make them separate. Or how to Bypass Google Cloud Storage Config for just Media Files. Thanks!

If you need more clarification please comment it out then I'll add more details as well.

Только что нашел решение. Это оказалось довольно просто, но у меня ушло много времени на то, чтобы сконфигурировать эту идею таким образом. Сейчас я использую Whitenoise для обслуживания всех статических файлов, поступающих из сборки React.js Frontend, а также для Django. Но Google Cloud Storage используется только для загрузки медиафайлов.

Это обновленный settings.py

#...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
GOOGLE_APPLICATION_CREDENTIALS = BASE_DIR / 'Cloud_Storage_Secrets/model-calling-323507-96957393f399.json'

STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = BASE_DIR / 'staticfiles'

MEDIA_URL = '/images/'
STATICFILES_DIRS = [
    BASE_DIR / 'static',
    BASE_DIR / 'build/static'
]

MEDIA_ROOT = BASE_DIR / 'images'
GS_BUCKET_NAME = 'buket-name'
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
# STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    GOOGLE_APPLICATION_CREDENTIALS
)
#...

Вы можете проверить работающий макет сайта с этой конфигурацией. https://naturalmedicineclinic.herokuapp.com/

Вернуться на верх