python - Heroku 上的基本 Django 静态文件部署

标签 python django heroku

enter image description here

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

"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()

我做错了什么?

最佳答案

我也遇到了类似的问题。将 STATIC_ROOT 更改为 BASE_DIR (从 PROJECT_ROOT)似乎有帮助。

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

关于python - Heroku 上的基本 Django 静态文件部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38275025/

相关文章:

python - "yield from iterable"与 "return iter(iterable)"

python - NLTK ConllCorpusReader 中的 NE 标签

python - 如何在 Django 中使用通用外键引用来引用对象

python - Django 的 collectstatic 有什么意义?

ruby-on-rails - Heroku 忽略最近的迁移

python - 亚马逊 MWS Boto get_matching_product_for_id

python - 从子线程 python 中杀死 main thead

python - 遵循 django 教程时出错

node.js - 如何在 mongoLab 和 Heroku 服务器中使用 mongo URI

javascript - Node.js 应用程序在 Heroku 上不断崩溃,给出错误引导超时