heroku上的基本Django静态文件部署

时间:2016-07-08 20:37:26

标签: python django heroku

enter image description here

我正在尝试将django项目部署到heroku(以前在openshift上),但无法正确获取静态文件。项目结构如上。在萤火虫中我得到了:

"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/css/bootstrap.min.css"
bootstrap.min.css
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/css/landing-page.css"
landing-page.css
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/jquery.js"
jquery.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/jquery-validate.js"
jquery-...date.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/bootstrap.min.js"
bootstrap.min.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/val.js"
val.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/font-awesome-4.2.0/css/font-awesome.min.css"

删节的settings.py文件:

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


....


# Application definition

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

MIDDLEWARE_CLASSES = (
    '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',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

....


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/


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

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'static/js'),
    os.path.join(BASE_DIR, 'static/css')
)

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, 'templates'),

mysite的/ wsgi.py:

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

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我有类似的问题。将STATIC_ROOT更改为BASE_DIR(来自PROJECT_ROOT)似乎有所帮助。

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
相关问题