Django Elasticbeanstalk:当 Debug = False 时无法访问静态文件

Posted

技术标签:

【中文标题】Django Elasticbeanstalk:当 Debug = False 时无法访问静态文件【英文标题】:Django Elasticbeanstalk: Cannot access static files when Debug = False 【发布时间】:2016-01-13 09:59:18 【问题描述】:

当 Debug = True 时,我的静态文件没有问题。我已经阅读了https://docs.djangoproject.com/en/1.8/howto/static-files/deployment/ 上的文档,但是当 Debug = False 时仍然无法使其工作。这些是我的设置(当 Debug = True 时工作正常):

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = 'myapp/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"myapp","static","static-only")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR),"myapp","static"),                 
)

我已将配置文件编辑为:

container_commands:
   collectstatic:
      command: "myapp/manage.py collectstatic --noinput"

option_settings:
  "aws:elasticbeanstalk:application:environment":
     DJANGO_SETTINGS_MODULE: "myapp.settings"
     PYTHONPATH: "/opt/python/current/app/myapp:$PYTHONPATH"
  "aws:elasticbeanstalk:container:python":
     WSGIPath: "myapp/myapp/wsgi.py"
  "aws:elasticbeanstalk:container:python:staticfiles":
    "/static/": "myapp/static/"

我一直在努力解决这个问题,所以任何帮助都将不胜感激!

【问题讨论】:

您是否运行了 collectstatic 命令,即python manage.py collectstatic 查看***.com/questions/5836674/… 【参考方案1】:

除了问你为什么要这样做之外,我没有直接回答你的问题。最佳做法是将您的静态文件移动到 S3 或理想情况下的 CloudFront(或其他非 AWS 解决方案)。

使用 django-storages-redux (https://github.com/jschneier/django-storages) 将来自 S3 的静态文件用于生产。谷歌“django s3 static”,你会发现几个解释这个过程的博客。文档是here。

【讨论】:

这个博客看起来不错:caktusgroup.com/blog/2014/11/10/…【参考方案2】:

collectstatic 命令会将所有静态文件收集到您设置为“/PATH/TO/myapp/static/static-only”的 STATIC_ROOT。

所以我怀疑您将静态文件指向了配置文件中的错误目录。

试试“/static/”:“myapp/static/static-only”

【讨论】:

以上是关于Django Elasticbeanstalk:当 Debug = False 时无法访问静态文件的主要内容,如果未能解决你的问题,请参考以下文章

Elastic Beanstalk 在哪里存储我的 Django 应用程序的文件?

ElasticBeanstalk Django:请提供 ENGINE 值

Django ElasticBeanstalk 更改存储桶

在 AWS Elasticbeanstalk 上部署 Django

Django manage.py 命令在 ElasticBeanstalk 上有 SyntaxError

在 Elasticbeanstalk Docker 环境中提供 Django 静态文件