csling
csling

Reputation: 328

Django admin static files not working on Heroku with whitenoise

I have a django site deployed to heroku and regular static files are working, but admin static files are not.

EDIT: I am using gunicorn but after some further research, it seems that I might need to use something like nginx in conjunction with gunicorn to accomplish what I'm looking for. Can anyone advise on that possibly?

Here's my settings.py

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'

STATIC_FILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATICFILES_DIRS = [os.path.join(PROJECT_ROOT, "staticfiles")]

These settings work to serve my normal static files, but like I said, the admin files are not working. I get a 404 in the console when it tries to load the static files. What I'm presented with when I go to the admin URL is a page with no css. Just raw browser displaying the login fields and title. I can't tell if it's a limitation of whitenoise and I need to host files elsewhere, or if I'm missing something. I'd prefer to keep everything within heroku as it's free for me now.

Upvotes: 4

Views: 1898

Answers (1)

sk139562
sk139562

Reputation: 31

I've been having this same problem - that my app works just fine, but the django admin app is not styling properly because it cannot find the static files when deployed on heroku (it renders just fine on my local machine)

I finally got it to work by running collectstatic on my local machine:

python manage.py collectstatic

This creates a static/ directory in my main project directory (i.e. the same directory where manage.py resides). I then moved the admin/ subdirectory to the /static/ directory (i.e. the static directory where settings.py resides). I then redeployed to heroku & now the admin site is styled correctly. Hope this helps!

Upvotes: 3

Related Questions