4

I'm trying to push my Django project to Heroku, but it isn't loading the staticfiles.

I used this to setup the things, everything is fine but I'm not able to fix the issue with static files.

My directory structure is like this

help_the_needy
    help_the_needy
        __init__.py
        settings.py
        urls.py
        views.py
        wsgi.py
    manage.py
    Procfile  
    requirements.txt  
    static
        css
        font-awesome
        fonts  
        img  
        js
    templates
        base.html
        display_list2.html
        index.html

Here is the complete code (all files).

This is my settings.py.

I tried alot of things to fix this, but nothing seems to work.

When I push it does copy static files but it's not loading them.

Can someone please point me to my mistake? Where is it wrong?

7
  • did you try to run the command python manage.py collectstatic --dry-run --noinput on the Heroku Django shell? do you get any error? Commented Mar 10, 2015 at 10:29
  • Yes. I got no error. (gyazo.com/30ea49aeb727054f5dd21265a45ae261). Commented Mar 10, 2015 at 10:31
  • u can try: STATIC_ROOT = 'staticfiles', STATIC_URL = '/static/', STATICFILES_DIRS = (os.path.join(BASE_DIR,'../static'),) TEMPLATE_DIRS = ( os.path.join(BASE_DIR, '../templates'),) Commented Mar 10, 2015 at 13:36
  • @LhAcKg It's not working, same output. Commented Mar 10, 2015 at 13:47
  • @TapasweniPathak your root project settings add this code import os and BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(file))) Commented Mar 10, 2015 at 17:47

6 Answers 6

9

I have been dealing with the same problem too. And here are the 2 things that I changed in my code.

(I'm using Django 1.7)

1) settings.py

I add these lines to the setting files

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,  'templates'),
    # Add to this list all the locations containing your static files 
)

STATIC_ROOT: this tells Django where to (a) put the static files when you run python manage.py collectstatic and (b) find the static files when you run the application

TEMPLATE_DIRS: this tells Django where to look for your static files when it search for statics files when you run python manage.py collectstatic

2) wsgi.py

Originally my file was:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

And I changed it to:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Read here for more information on whitenoise: https://devcenter.heroku.com/articles/django-assets#whitenoise


Also, remember to install whitenoise: pip install whitenoise==2.0.6

Before deploying the project, run: python manage.py collectstatic

This will create a folder indicated by STATIC_ROOT (declared in your settings.py), containing all your static files.

Sign up to request clarification or add additional context in comments.

1 Comment

Since it's been a few years since this answer was posted, here's what worked for me in Django 3.2.
4

Since it's been a few years since this was posted (and it still pops up when I search for the issue), here's what worked for me in Django 3.2.

pip install whitenoise

Make sure in settings.py you have

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'),
)

Add whitenoise to your Middleware:

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

Make sure the directory you specified as the STATIC_ROOT (staticfiles) exists in your base directory and is not empty.

After that, I committed the changes and Heroku was able to build the static files and everything worked fine.

Comments

2
pip install whitenoise    

Add whitenoise to requirement.txt.

and also add whitenoise in the middleware of settings.py

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', #add whitenoise
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Follow the steps in https://devcenter.heroku.com/articles/django-assets

Comments

0

Your STATICFILES_DIRS setting is wrong. It should be pointing to the actual location of the "static" directory containing the files:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

2 Comments

I did that before, it was not working. I tried os.path.join(BASE_DIR, '../static') too, it too didn't worked. I tried both the things again, same output.
anything else to fix this?
-1

It looks like django don't know where your static_root is. This folder is autogenerated when you fire manage.py collectstatic Make sure that the folders static and templates are inside your django app. You didn't created a django app to put this folders into. You created a django project the next step is to create a django app with something like this python manage.py startapp polls

# Absolute filesystem path to the Django project directory:
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
# Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT)

STATIC_URL = '/static/'
STATIC_ROOT = normpath(join(SITE_ROOT, 'static_root'))

Comments

-1

Your urls.py file lacks the setting to manage and route requests for static resources.

in order to provide access to static resorces you must add to urlpatterns of urls.py:

urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

3 Comments

That is for development only.
@DanielRoseman i know but Heroku requires this configuration, another answer can be found here. stackoverflow.com/questions/9047054/…
No, that linked answer is simply wrong. The correct answer is provided by Heroku's own documentation and the OP is already following it most of the way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.