Static files not found - Deploying Django on Heroku

时光毁灭记忆、已成空白 提交于 2019-12-30 11:13:29

问题


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. I continue to get errors like this in my log:

I'm not sure if I am referencing the paths properly. 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, or is there some other issue that could be causing this?

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' %}

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.


回答1:


Install dj-static package

$ pip install dj-static

Configure your static assets in settings.py:

DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

Then, update your wsgi.py file to use dj-static:

import os

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

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

application = Cling(get_wsgi_application())


来源:https://stackoverflow.com/questions/34121663/static-files-not-found-deploying-django-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!