我知道這個問題被多次提出,但無法解決問題。這是我的問題:我的 Django-React 應用程式部署在 Heroku 上并且運行良好(非常簡單的應用程式)。我想知道訪問我的應用程式的 /admin 部分,但我收到 500 內部服務器錯誤。該錯誤出現在本地和 Heroku 中。DEBUG 是錯誤的,不幸的是我無法讓日志作業,無論是在 Heroku 還是在本地:(
這是我的settings.py:
from pathlib import Path
import django_heroku
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxx'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['vmbf.herokuapp.com', '127.0.0.1', 'localhost']
ADMINS = [('username', 'emailaddress')]
MANAGERS = ADMINSEMAIL_HOST = 'host'
SEND_BROKEN_LINK_EMAILS=True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'emailaddress'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = EMAIL_HOST_USER
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'students',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'corsheaders.middleware.CorsMiddleware',
'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 = 'django_react_proj.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'students-fe/build')],
'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 = 'django_react_proj.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
django_heroku.settings(locals())
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOWED_ORIGINS = (
'https://vmbf.herokuapp.com',
)
CSRF_TRUSTED_ORIGINS = [
'vmbf.herokuapp.com',
]
STATIC_ROOT = os.path.join(BASE_DIR,'students-fe', 'build', 'static')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
這是我的檔案夾的樣子:
檔案夾結構
隨意問我更多代碼,我是 Django / React 的新手,因此我不確定我應該在這里分享什么。
uj5u.com熱心網友回復:
所以我發現了我的問題。
它與 PostgreSQL 無關,但感謝 @Chris 指出這一點,這對我自己來說是更好的開發方法。
問題與我如何管理 settings.py 中的靜態檔案有關
我不明白 STATIC_ROOT 是如何作業的,我理解相反。在反思之后,這是我所做的:
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
我的檔案將從這個staticfiles目錄提供。
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'students-fe','build','static'),
]
但它必須包含我在學生-fe/build/static 檔案夾中的反應應用程式。
最重要的是,在部署到 Heroku 時,有必要添加 buildpack,我這樣做了,但順序不正確。
首先,我需要添加 python(用于 Django):
heroku buildpacks:set heroku/python
然后說在這之前它需要安裝 nodejs(用于 React):
heroku buildpacks:add --index 1 heroku/nodejs
像這樣 Heroku 將首先在python manage.py colecstatic之前執行npm run build,這是將靜態檔案移動到 staticfiles 檔案夾所需的。
希望它可以幫助其他人。感謝大家的回答。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393365.html
標籤:姜戈 英雄联盟 django-admin
