在 Render.com 上部署期间,我遇到服务器错误 (500),但在日志中未收到任何警告或错误。有趣的是,只有不与数据库交互的页面才能成功加载,这表明数据库权限或连接配置存在潜在问题。不幸的是,由于缺乏详细的错误消息,因此很难查明问题的确切原因。寻求见解和解决方案来解决此部署问题。
from pathlib import Path
import os
import dj_database_url
# 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/5.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', default='your secret key')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = 'RENDER' not in os.environ
ALLOWED_HOSTS = []
RENDER_EXTERNAL_HOSTNAME = os.environ.get('RENDER_EXTERNAL_HOSTNAME')
if RENDER_EXTERNAL_HOSTNAME:
ALLOWED_HOSTS.append(RENDER_EXTERNAL_HOSTNAME)
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'catalogoApp',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'panelBM.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'panelBM.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
DATABASES = {
'default': dj_database_url.config(
# Feel free to alter this value to suit your needs.
default='postgresql://postgres:postgres@localhost:5432/mysite',
conn_max_age=600
)
}
# Password validation
# https://docs.djangoproject.com/en/5.0/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/5.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/
STATIC_URL = 'static/'
if not DEBUG: # Tell Django to copy statics to the `staticfiles` directory
# in your application directory on Render.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_ROOT = BASE_DIR / "files"
MEDIA_URL = "/media-files/"
起初我遇到了 staticFiles 文件夹的警告问题,但我用命令 py manage.pycollectstatic 解决了它,然后我遇到了服务器错误(500)的问题,我尝试将 ALLOWED_HOSTS 更改为 ['*'] ,但没有成功。
看起来您已将数据库 URL 硬编码到本地主机数据库:
DATABASES = {
'default': dj_database_url.config(
# Feel free to alter this value to suit your needs.
default='postgresql://postgres:postgres@localhost:5432/mysite',
在生产中,您需要使用生产渲染数据库的 URL。
听起来您的 Django Web 服务和Postgres DB 都部署在 Render 上。如果您将它们部署在同一区域,则 Web 服务可以使用数据库的“内部 URL”连接到数据库:https://docs.render.com/databases#connecting-with-the-internal-url
否则,您必须使用数据库的“外部 URL”。
您不想将数据库连接字符串放入代码中,因为它包含密码。
相反,您可以将数据库连接字符串放入环境变量中。所以你的代码可能看起来像这样:
DATABASES = {
'default': dj_database_url.config(
default=os.environ["DB_URL"],
然后
DB_URL
环境变量。DB_URL
环境变量。具体方法如下:https://docs.render.com/configure-environment-variables