1

I am trying to deploy a Django site on Heroku, but I'm running into problems getting the app to locate my static files. I have used python manage.py collectstatic to collect my static files into a staticfiles folder, but my app still doesn't seem to be able to find them.

My settings.py looks like this:

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

DEBUG = False

ALLOWED_HOSTS = ['www.tomdeldridge.com']

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'tomdeldridge.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['tomdeldridge/templates/tomdeldridge/'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'tomdeldridge.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATICFILES_DIRS = (
    os.path.join(
        os.path.dirname(__file__),
        'static',
    ),
)

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

My wsgi.py file:

import os

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tomdeldridge.settings")

My directory structure:

The images I'm trying to reference in my template definitely exist (they work fine when I run the app locally.) I reference them like this:

{% static 'tomdeldridge/images/computer-2.png' %}

But I get an error that looks something like this:

I'm not sure if I am referencing these paths properly during deployment though. The path's that are set to images/stylesheets/scripts in the code are using the path to the original static folder used in development. Do I have to rewrite all of those paths to point to the new staticfiles folder I created with the collectstatic command? That seems like an unnecessary hassle...

Or do I have to use a server like nginx to serve static files in deployment? I am totally lost on where to go from here, and I'm not really sure why it's necessary to reconfigure the entire static file structure just to deploy.

3
  • 1
    Note that you must absolutely not use sqlite on Heroku; the db file would be stored on the dyno itself and would not survive across restarts. Commented Jan 12, 2016 at 9:07
  • Well, your settings file there shows you are using sqlite. If there is a difference between that and your actual settings file with regards to db settings, maybe there is also a difference when it comes to static settings? Commented Jan 12, 2016 at 9:14
  • Actually you're totally right, it is sqlite. (I got confused looking at my Heroku profile which has info on Postgres.) Thanks for letting me know that won't work on Heroku, but does the db have any effect on serving static files? Or is that just another unrelated issue you spotted? Commented Jan 12, 2016 at 21:53

1 Answer 1

1

I'd rather add this as a comment but my karma prevents me from doing so;

My initial observation would be that the permissions aren't set correctly, so the server can't find the files correctly.

Do the files/folder have the same permissions as the development server?

One other question, does the os.path.dirname(__file__) correctly resolve to the project directory? Your BASE_DIRis referencing the same place, so try printing this on startup by putting:

print "BASE Directory:", BASE_DIR

On startup this should print out the directory its looking at as your project folder.

Check these both these things first and let me know the results of the above printout.

Edit, I'm not too familiar with Heroku but I'm starting with the basics (nima's solution below might work).

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

4 Comments

BASE_DIR points to the right directory. I'm not sure exactly what you mean when talking about permissions. I haven't set any special permissions if that's what you're asking.
Wait, it looks like your static folders are inside the app rather then the project folder. Shouldn't the folders be beside your manage.py file Project folder, not app folder)?
I was under the impression that is acceptable to do. collectstatic successfully collects all my static files and puts them into a top level directory in the project called staticfiles.
I always had issues with using the collectstatic in my deployment experience (albeit short). Let me investigate a little more when I'm at home and can actually see how I've deployed things in the past. Sorry I can't be more help right now!

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.