亚马逊网络服务 (AWS) 上的 Django
Django on Amazon Web Service (AWS)
我已经阅读了这些教程:https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/ and http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html
我使用的是 Django 1.8.2,亚马逊的文档似乎有点过时(他们仍然使用 django-admin.py 而不是 django-admin),并且其中的某些部分无法正常工作(当东西不起作用时,我回退到 realpython link 一个)。
所以,除了我的管理页面不加载静态文件外,我的一切正常。因此,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 文档文件,例如 https://docs.djangoproject.com/en/1.8/howto/static-files/,但我不确定要为 AWSSTATIC_ROOT 放入什么
非常感谢任何帮助。谢谢
事实证明 "aws:elasticbeanstalk:container:python:staticfiles" 将您的 EC2 实例 (/opt/python/current/app/static/) 目录中的文件映射到 /static/
将 settings.py 中的 STATIC_ROOT 设置为 os.path.join(BASE_DIR, '..','static') 解决了问题
虽然你似乎已经解决了你的问题,但我也遇到了类似的问题,但是由于我的应用程序是从项目目录的根目录上传的,所以设置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
不知何故指向了一个奇怪的位置
You have requested to collect static files at the destination
location as specified in your settings:
/opt/python/bundle/2/app/static
上述问题也都在将命令更改为AWS Docs版本后得到解决。
我已经阅读了这些教程:https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/ and http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html
我使用的是 Django 1.8.2,亚马逊的文档似乎有点过时(他们仍然使用 django-admin.py 而不是 django-admin),并且其中的某些部分无法正常工作(当东西不起作用时,我回退到 realpython link 一个)。
所以,除了我的管理页面不加载静态文件外,我的一切正常。因此,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 文档文件,例如 https://docs.djangoproject.com/en/1.8/howto/static-files/,但我不确定要为 AWSSTATIC_ROOT 放入什么
非常感谢任何帮助。谢谢
事实证明 "aws:elasticbeanstalk:container:python:staticfiles" 将您的 EC2 实例 (/opt/python/current/app/static/) 目录中的文件映射到 /static/
将 settings.py 中的 STATIC_ROOT 设置为 os.path.join(BASE_DIR, '..','static') 解决了问题
虽然你似乎已经解决了你的问题,但我也遇到了类似的问题,但是由于我的应用程序是从项目目录的根目录上传的,所以设置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
不知何故指向了一个奇怪的位置
You have requested to collect static files at the destination location as specified in your settings:
/opt/python/bundle/2/app/static
上述问题也都在将命令更改为AWS Docs版本后得到解决。