35๐
Just so you guys know, namespace for static files in recent versions of EBS, changed to aws:elasticbeanstalk:environment:proxy:staticfiles
, like this:
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
25๐
I came across the same problem today, and I realized that I forgot this option in the .ebextensions/.config file. Make sure you have it too
option_settings:
- namespace: aws:elasticbeanstalk:container:python:staticfiles
option_name: /static/
value: static/
- [Django]-How can I apply a filter to a nested resource in Django REST framework?
- [Django]-Django โ view sql query without publishing migrations
- [Django]-What is the format in which Django passwords are stored in the database?
18๐
To support multiple apps and do this you need to run collectstatic
Settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static")
Make sure you have a folder called โstaticโ in your root
In your ebs config file eg. (02_python.config or similar)
option_settings:
...
"aws:elasticbeanstalk:container:python:staticfiles":
/static/: "static/"
Then before you upload to ebs run python manage.py collectstatic
This collects all the static files in one folder which you have already pointed to in your config.
Then you can run eb deploy
like normal
Opptionally if you donโt want to commit the static files to your source control twice and want the server to do this for you add this to your config
container_commands:
01_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"
So your file should look something like this:
container_commands:
01_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: app/wsgi.py
"aws:elasticbeanstalk:container:python:staticfiles":
/static/: "static/"
This will run collect static for you when you run eb deploy
- [Django]-Django MySQL error when creating tables
- [Django]-How can I iterate over ManyToManyField?
- [Django]-How to use UUID
13๐
If your environment uses a platform branch based on Amazon Linux 2, the right settings for
.config file inside .ebextensions folder
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
Inside your project settings.py you should have:
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
- [Django]-How to render form field with information that it is required
- [Django]-Install mysql-python (Windows)
- [Django]-Returning data on POST in django-tastypie
7๐
For me, the problem was having
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
Instead, I changed it to
STATIC_ROOT = 'static'
Also, my .conf file has
option_settings:
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
- [Django]-How to omit object name from Django's TabularInline admin view?
- [Django]-Django Model MultipleChoice
- [Django]-Django-rest-framework http put failing with 415 on django 1.5
2๐
All previous answers didnโt help me
This works for me.
Basically I created two steps inside .ebextensions
01_django.config
container_commands:
01_migrate:
command: "source /opt/python/current/env && source /opt/python/run/venv/bin/activate && cd /opt/python/current/app && python manage.py migrate --noinput"
leader_only: true
02_touch_superuser:
command: "source /opt/python/current/env && source /opt/python/run/venv/bin/activate && cd /opt/python/current/app && python manage.py touch_superuser"
leader_only: true
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: config/wsgi.py
NumProcesses: 2
NumThreads: 10
aws:elasticbeanstalk:application:environment:
STAGING: 1
DJANGO_SETTINGS_MODULE: config.settings.production
aws:elasticbeanstalk:container:python:staticfiles:
"/static/": "htdocs/static/"
"/media/": "htdocs/media/"
config/wsgi.py
Could be a different path in your project
02_collec_static.config
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/10_collect_static.sh":
mode: "000755"
owner: root
group: root
content: |
set -xe
source /opt/python/current/env
source /opt/python/run/venv/bin/activate
cd /opt/python/current/app && python manage.py collectstatic --noinput
echo "Statics collected...!!"
An important thing, you settings/*.py
should match with your static path that EBS serves, in my case this is my config.
...
PROJECT_PATH = dirname(dirname(dirname(__file__)))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'htdocs/media')
STATIC_ROOT = os.path.join(PROJECT_PATH, 'htdocs/static')
...
- [Django]-List field in model?
- [Django]-Django reverse lookup of foreign keys
- [Django]-Django: how to access current request user in ModelForm?
2๐
Add a file name static-files.config under .ebextensions, and add below content:
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
That works for me. I am using django2.2 + python 3.7
For more detail please check:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-cfg-staticfiles.html#environment-cfg-staticfiles.namespace
- [Django]-How do I get the object if it exists, or None if it does not exist in Django?
- [Django]-Django โ cannot import name 'config' from 'decouple'
- [Django]-Django: How to override form.save()?
1๐
I did the following to fix static path in beanstalk
STATIC_URL = '/static/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
option_settings:
...
...
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
- [Django]-Separation of business logic and data access in django
- [Django]-Serving gzipped content from django
- [Django]-UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 2: ordinal not in range(128)
1๐
I struggled for quite a while on that, thinking that the issue was with :
option_settings:
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
But actually my issue was with the other commands in the xxx.config file.
basicaly, make sure the other lines are correct.
If you want to know my personal setup, I used the settings file shown above and I added the static directory in the root of my project.
For the settings.py file here is what I had for the static_url and root :
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
Hope it helps !
- [Django]-Filter Queryset on empty ImageField
- [Django]-How to correct this error: "'Adminsite' object has no attribute 'root'"
- [Django]-Embedding JSON objects in script tags