gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:39 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com