gpt4 book ai didi

python - 当 DEBUG=False 时,Django 不在 Heroku 上提供静态文件

转载 作者:行者123 更新时间:2023-12-05 07:05:18 25 4
gpt4 key购买 nike

在解释问题之前,我想告诉你,我已经在 StackOverflow 上尝试过这些解决方案:

  1. Django Heroku not serving static files when Debug=False
  2. Django: Whitenoise not working in production with debug false

Heroku 票证

我也在 Heroku 上提出了问题。我得到了这个回复:

Please try running the command python manage.py collectstatic on your local machine before deploying your app to Heroku. This would make the required arrangements in your application directory which you can later push to Heroku.

The reason why it isn't working is due to the ephemeral file system of Heroku. This means that any changes made to the file system will not be persistent. Every dyno boots with a fresh copy of your deployed application file system. When you run this command from the Heroku bash, it spins a one-off dyno and runs the task on this dyno. So, once you run the task and exit the bash, these changes are deleted. They do not reflect on your application's live dyno. This is why you need to make all the file system changes on your local machine and push them to your Heroku app.

Furthermore, when you set the DEBUG=false in your settings.py, it doesn't display any internal error log or data when you browse your app. Displaying this information on your production app is not ideal. This is why it is suggested to set DEBUG=false. If your application is still in the testing phases, you can try setting it to true. But, please make sure you make it false before your final release.

我的回应是:

  1. 在我的本地机器上运行 python manage.py collecstatic
  2. 使用 staticfiles/ 文件夹中的新文件将我的代码推送到 GitHub 存储库
  3. 设置 DEBUG=False。但是,它仍然显示服务器错误 (500)。

现在他们说问题出在我的应用程序上,他们无法在应用程序级别帮助我。所以,我又回到了这里。

最小生产

  1. production.py
import os
import django_heroku
import dj_database_url
import dotenv

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

dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
dotenv.load_dotenv(dotenv_file)


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# Application definition

INSTALLED_APPS = [
'home',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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 = 'portfolio.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'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',
],
},
},
]

STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

STATIC_ROOT = os.path.join(BASE_DIR, "live-static", "static-root")

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, "live-static", "media-root")

django_heroku.settings(locals())
  1. 目录结构
.
├── home
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── live-static
│ ├── media-root/
│ └── static-root/
├── manage.py
├── portfolio
│ ├── settings
│ │ ├── base.py
│ │ ├── __init__.py
│ │ ├── local.py
│ │ └── production.py
│ └── urls.py
├── Procfile
├── requirements.txt
├── runtime.txt
├── static/
├── staticfiles/
└── templates/
  1. requirements.txt:

whitenoise==5.1.0

最佳答案

manage.py 和 wgsi 文件中的路径是否更改为生产设置?

在 wgsi 中进行此更改。

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'portfolio.settings.production')

并且在 manage.py 中也进行了此更改。

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'portfolio.settings.production')

关于python - 当 DEBUG=False 时,Django 不在 Heroku 上提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62776756/

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