I have deployed my Django app in amazon elastic beanstalk. However, the css in the django admin is not working. I understand that it's a collectstatic issue. This is what my settings.py looks like:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
And I have this line in my ebconfig.config file:
container_commands:
01_collectstatic:
command: "python manage.py collectstatic --noinput"
However, this is how it looks like once I deploy it in amazon:
I have tried seeing the log files in amazon console. I found a line that reads:
EmbeddedPostBuild/postbuild_0_dcape/Command 01_collectstatic] : Starting activity...
.....
0 static files copied to '/opt/python/bundle/33/app/static', 102 unmodified.
What do I need to do to make sure it works?
I was having the same issue for the longest time. It seems like you have to tell beanstalk where your static folder will be located on the production server.
In your ebconfig.config where you specify commands/container_commands add an option settings so that beanstalk can know what your static_root equates to on the live server.
This is how I do it:
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
config file
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: MDGOnline.settings
"aws:elasticbeanstalk:container:python:staticfiles":
"static/": "www/static/"
Check out this wonderful AWS Elastic Beanstalk Doc for more detailed info
Related
I am making a small Django app and I want to use files uploaded via Django admin panel in production
This is from my settings.py file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = ''
MEDIA_ROOT = BASE_DIR
I trust admin uploaded files and want to serve them easily(without Amazon S3 and similar services), so i tried to place the uploaded files with other static files
image = models.ImageField(upload_to='static/images')
After uploading an image via admin panel i run python manage.py collectstatic in the DigitalOcean console. Whenever i try to access the image i get Error 404, while all other static files(which are real static files, not uploaded via admin panel) load successfully
I did the same thing locally and there is no problem, everything loads as expected(DEBUG=True is set both locally and on DigitalOcean). Is it some security measure that doesnt let uploaded files end up in static? How can i bypass it if i trust files uploaded via admin panel?
if you're using manage.py runserver to run you website
first make sure that the media is being served in your main urls.py
urlpatterns = [
...
] + [
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
]
then you can use runserver with --insecure to run your website with or without DEBUG=True
python manage.py runserver 0.0.0.0:80 --insecure
https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/#cmdoption-runserver-insecure
as in the django documentation this is not the recommended way to serve your files, you should use a reverse proxy like nginx or apache
or if you really dont want to use a reverse proxy, you could give whitenoise a try
http://whitenoise.evans.io/en/stable/django.html
I uploaded the my whole application on heroku but I have a problem does not display any photos. The path to the pictures is defined in this way:
src="{{blog.image.url}}"
Therefore, the view should not only be local (I think so).
After uploading the application, my view of django.admin it also looks strange.
Any help will be appreciated! How can I solve this ( to be pictures will be displayed)? Can I do it without re-uploading the application (my database is completed)?
If someone meet the same problem:
This provides support for files such as CSS in Heroku
1.) In file settings.py just add the following code:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Important links at this stage:
https://devcenter.heroku.com/articles/django-assets
Provides multimedia showing
2.) In settings.py would add:
#<-------------Elements Amazon S3 Beginning --------->
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'reviews/static'),
] #ok
AWS_ACCESS_KEY_ID = 'YYY'
AWS_SECRET_ACCESS_KEY = 'XXX'
AWS_STORAGE_BUCKET_NAME = 'name'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
#AWS_LOCATION = 'static' #ok
#STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' #OK
#STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION) # już wystęuje
DEFAULT_FILE_STORAGE = 'winerama.storage_backends.MediaStorage' # <-- here is where we reference it
#<-------------Elements Amazon S3 END -------------->
2.1) In the same folder add a file storage_backends.py
#FIle for amazon 3s
from storages.backends.s3boto3 import S3Boto3Storage
class MediaStorage(S3Boto3Storage):
location = 'media'
file_overwrite = False
*of course, we also have to create amazon 3s account.
Important links at this stage:
https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html (very good tutorial how to use amazon 3s to media files and static files)
2.2)I also use these commands when adding an application to Heroku:
heroku config:set AWS_ACCESS_KEY_ID=XXX AWS_SECRET_ACCESS_KEY=YYY
heroku config:set S3_BUCKET_NAME=s'name'
Important links at this stage:
https://devcenter.heroku.com/articles/s3
3.) In deploy it helped me
https://tutorial-extensions.djangogirls.org/en/heroku/
(I think the link is helpful but to deploy the whole application help me the following answer in the forum)
disable the collectstatic during a deploy
heroku config:set DISABLE_COLLECTSTATIC=1
deploy
git push heroku master
run migrations (django 1.10 added at least one)
heroku run python manage.py migrate
run collectstatic using bower
heroku run 'bower install --config.interactive=false;grunt prep;python manage.py collectstatic --noinput'
enable collecstatic for future deploys
heroku config:unset DISABLE_COLLECTSTATIC
try it on your own (optional)
heroku run python manage.py collectstatic
Author of the statement tomcounsell
Link: Collectstatic error while deploying Django app to Heroku
Please forgive my bad English.
My project runs fine on the local sever but during deployment on Heroku, I get the following error
Collectstatic configuration error. To debug, run: $ heroku run python manage.py collectstatic --noinput
I understand this is due to STATIC_ROOT settings in settings.py, but I can't seem to point to the location of the static files properly.
This is my folder structure.
C:/
/Users
/Mac
/denv
/musicpro
/musicpro
/featureapp
/static
/featureapp
/css
/img
/js
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'musicpro/featureapp/static/featureapp/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static")
)
I'm getting 404 for static files when DEBUG=False in my settings. The file is served fine when DEBUG=True.
I'm looking at the docs and all my configs seem OK to me.
# my_settings.py
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/`
then I run collectstatic --settings=my_settings, and ls static to confirm it has collected the files. It has:
ls ls static/css/core.css
# (its there).
But when we request "localhost:8000/static/css/core.css" we get 404.
Note running python manage.py findstatic css/core.css --settings=my_settings fails to find the file. When I do DEBUG=True and run findstatic it finds the file, but in the sub app's static dir, not the collected directory (STATIC_ROOT).
Note I have:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
and "staticfiles" is in my INSTALLED_APPS.
Django 1.6
As you have specified DEBUG = FALSE in settings, its now left on HTTP server like nginx or apache to serve the static files. You need to configure your webserver to serve static files. An example for apache can be found here.
This si my settings.py about static in settings.py:
STATIC_ROOT = '/home/coat/www/site/app/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"/usr/lib/python2.6/site-packages/django/contrib/admin/static/",
# This is Django admin default static files
)
I user django server:
./manager runserver
Then I open the URL: http://localhost:8000/static/admin/css/base.css
It works very well.
But a open http://localhost/static/admin/css/base.css
It print '404'
I had restart Nginx and uwsgi for many times, but it dosen't works.
First things first, this is nonono:
STATIC_ROOT = '/home/coat/www/site/app/static/'
Never hardcode absolute paths, you're just making your settings file less portable and probably killing kittens. Adapt this to your needs:
import os.path
import posixpath
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# fix STATICFILES_DIRS too
Now, to your question. django.contrib.staticfiles is fantastic but probably a little confusing at first.
You must understand the collectstatic command:
Collects the static files into STATIC_ROOT. [...] Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.
With runserver, staticfiles are served automatically, but in production mode (DEBUG=False, real HTTP server like Nginx), you should run collectstatic to (re)build STATIC_ROOT
STATIC_ROOT: is the root path where the HTTP server should serve static files from.
STATIC_URL: is the root URL where the HTTP server should serve static files to.
STATICFILES_DIRS: other static directories, in addition to each app's "static" subdirectory. Because django.contrib.admin is a normal app with a "static" folder, there is no need to specify it in the settings.
Conclusion: if STATIC_ROOT resolves to /home/coat/www/site/app/static/, and STATIC_URL is /static/, then you should:
Run collectstatic management command
Configure Nginx to serve /home/coat/www/site/app/static/ on /static/, ie.:
location ^~ /static/ {
alias /home/coat/www/site/app/static/;
}
Reload nginx
In development the runserver command does some magic to serve your static files. In production you need to configure NGinx to do it. There are two chapters in the Django documentation I recommend you read:
Serving static files in production
Serving files (Deployment)
Especially the last link explains how you have to configure your NGinx to serve your static files.