当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Static files not found - Deploying Django on Heroku

问题描述:

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.

网友答案:

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())
分享给朋友:
您可能感兴趣的文章:
随机阅读: