我在 Google Compute Engine 上托管了我的 Django 应用程序。我希望从 Google Cloud Storage 呈现应用程序的静态元素。我在 Google Cloud 存储桶 www.example.com/static 中拥有所有静态元素
我的设置.py:
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '../example_static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '../example_media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), MEDIA_ROOT,)
000-default.conf 文件:
<VirtualHost *:80>
.....
DocumentRoot /var/www/html
Alias /static /opt/projects/example-google/example_static
....
</VirtualHost>
使用当前设置,它从路径中获取静态文件:/opt/projects/example-google/example_static。
有人可以解释一下从 Google Cloud 存储桶 www.example.com/static 渲染所有静态图像所需的设置更改吗?
谢谢,
最佳答案
你可以找到一些文档 here
我发现另一件有用的事情是通过在您的应用程序 settings.py 中进行以下更改来自动在开发环境和生产环境之间切换:
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
STATIC_URL = 'https://storage.googleapis.com/<your-bucket>/static/'
else:
STATIC_URL = '/static/'
关于python - 用于从 Google 云存储呈现静态图像的 Django 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39422723/