我已阅读这些教程:https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/和http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html
我使用Django 1.8.2似乎亚马逊的doc有点过时了(他们仍然使用django-admin.py而不是django-admin),而且它的某些部分不起作用(当东西不起作用时,我会回到realpython链接上)。
所以,除非我的管理页面没有加载静态文件,否则我完成了所有工作。因此,未加载css文件。
这是我的settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
我也尝试过使用:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static').
但它仍然不起作用。
这是我的eb配置文件:
container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python papp/manage.py migrate --noinput"
leader_only: true
02_createsuperuser:
command: "source /opt/python/run/venv/bin/activate && python papp/manage.py createsu"
leader_only: true
03_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python papp/manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "papp.settings"
PYTHONPATH: "/opt/python/current/app/papp:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: "papp/papp/wsgi.py"
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
我在进行更改后使用了eb deploy命令。
我需要做额外的步骤吗?我在这里读到:Default Django 1.5 admin css not working我需要更改别名,但它适用于apache。
我也读过django doc文件,例如https://docs.djangoproject.com/en/1.8/howto/static-files/,但我不确定要在AWS的STATIC_ROOT中放什么
非常感谢任何帮助。感谢
答案 0 :(得分:3)
原来,“aws:elasticbeanstalk:container:python:staticfiles”将EC2实例目录中的文件(/ opt / python / current / app / static / )映射到/ static /
将settings.py中的STATIC_ROOT设置为os.path.join(BASE_DIR,'..','static')修复了问题
答案 1 :(得分:3)
虽然您似乎已经解决了问题,但我遇到了类似的问题,但由于我的应用程序是从项目目录的根目录上传的,因此设置STATIC_ROOT = os.path.join(BASE_DIR, '..','static')
不起作用。
更改container_commands以遵守AWS Docs的做法
container_commands:
01_migrate:
command: "django-admin.py migrate"
leader_only: true
02_collectstatic:
command: "django-admin.py collectstatic --noinput"
leader_only: true
在此之前,在同一个教程之后,我遇到了以下问题
createsu
命令无效。
运行
$ eb ssh
$ /opt/python/run/venv/bin/python manage.py collectstatic
以某种方式指向一个奇怪的位置
您已请求在目的地收集静态文件 您在设置中指定的位置:
/opt/python/bundle/2/app/static
将命令更改为AWS Docs版本后,所有上述问题也得到了解决。