Whitenoise doesn't seem to help images to show up in heroku - django

I got the 404 error to images.
https://example.herokuapp.com/images/IMG_2060.JPG 404 (Not Found)
This is my tree of directory.
directory1
|
|-- manage.py
|
|-- build/
| |
| |--static/
| .
| .
|
|-- static/
. |
. |--images/
. .
. .
The images that I want to see are in directory1/static/images/.
settings.py
INSTALLED_APPS = [
'whitenoise.runserver_nostatic',
...
]
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
....
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
whitenoise version is 5.2.0. I deployed this app to Heroku. Everything works fine except images. Did I miss something?
Thank you in advance! :)

Django's static file handling can be a bit confusing.
STATIC_ROOT is supposed to be an empty directory which Django copies your static files to ready for serving. This is done by the collectstatic command which is run automatically by Heroku.
Directories you want it to copy files from should be listed in STATICFILES_DIRS.
So if you change your settings like this it should work:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static'),
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')

Related

Django Admin CSS is not working as it should

I did run the python manage.py collectstatic code, and after that all the static files were collected in the static folder. but the static files needed for the syling of the admin panel are not working. Could you help?
settings.py
STATIC_URL = 'static/'
STATICFILES_DIRS = [
'Portfolio/static/',
'Portfolio/',
''
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Comment out the STATIC_ROOT
I think your STATICFILES_DIRS is wrong change it to os.path.join(BASE_DIR, 'static')
Try to change STATIC_URL. for example: /Portfolio-Deployment-Folder/static/
Try to change localhost port for example:
python3 manage.py runserver 9000 or 7000
most probably problem of STATICFILES_DIRS
And finally a full example for STATIC in my personal project:
STATIC_URL = '/myproject/static/'
# STATIC_ROOT = BASE_DIR / 'static'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]

Gunicorn cant find the static files

By using gunicorn without nginx from digitalcloud tutorial
my server is runing and on the console is
not found: /static/style.css
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
I already tried to
collectstatic
do urlpatterns += staticfiles_urlpatterns() in urls.py file
makemigrations + migrate
maybe django try read 'static//static/style.css' but you need this: 'style.css' ;) ?
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
urlpatterns = [your urls here] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can create in your project folder settings then in them create settings files like names:
local.py
from my_project.settings import *
DEBUG = True
ALLOWED_HOSTS = []
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this run in VM like: python manage.py runserver --settings settings.local
remember in urls add:
if settings.DEBUG == True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
web.py
from my_project.settings import *
DEBUG = False
ALLOWED_HOSTS = ["www.side.com", "side.com"]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and then in gunicorn import only web.py file project to read in nginx
this run in VPS server.

Seperate folder for images

I have an API that delivers images and puts them in a folder. I want to make that folder accessible via Django.
folder structure:
- theProject
- static
settings.py
urls.py
.
.
.
- theApp
- apiimagesfolder
views.py
forms.py
urls.py
.
.
.
I tried adding the apiimagesfolder folder to my static dirs:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "theProject", "static"),
os.path.join(BASE_DIR, "theApp", "apiimagesfolder"),
]
and I tried accessing an image in apiimagesfolder on the debug server with 127.0.0.1:8000/imagename.bmp but failed. What would the correct path be?
Is the path staying the same after deploying?
Try this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "apiimagesfolder"),
]

Static file not load in django template?

Problem is STatic file not load.
CRM/
| |-- CRM/
| |-- next_crm/
| |-- static/
| | +-- css/
| | +-- bootstrap.min.css <-- here
| +-- manage.py
This is my static variables defined in the
next_crm/settings/settings.py file.
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
In your primary project folder (CRM, in this case) you have added the urls.py static debug lines?
urls.py
from django.conf import settings
...
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My settings.py are:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
I also face this type of problem few days ago.
but found a solution for this.Add these lines to load static in settings.py
STATICFILE_DIRS = (os.path.join(BASE_DIR, "static"), "static")
instead of this:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
Hope this will solve your problem. :)

django The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

I'm deploying a Django application on heroku.
In my settings module, I have configured to host static files like
STATIC_ROOT = os.path.join(BASE_DIR, 'static_my_project')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_my_project')
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media_root')
and urls.py
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But on deployment to heroku, it gives error as
SystemCheckError: System check identified some issues:
ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
may be help this.
STATIC_URL = '/static/'
if not DEBUG:
STATIC_ROOT = ''
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
The problem is in the STATIC_ROOT and STATICFILES_DIRS there names cant be same.
static root is used for collectstatic command of django.
You can remove it if not using it.
And can also remove static files dirs if not changing its default position(inside django app) to somewhere else like base or something.