![](/img/trans.png)
[英]Server error 500 on Django when deployed on Heroku when debug is set to False
[英]500 error on deployed django app with debug set to False
我知道以前有人問過這個問題,但我已經完成了其他帖子中推薦的所有內容,但我仍然遇到了這個問題。
我添加了必要的允許主機設置 postgres,這樣我就不會在生產白噪聲設置中使用 sqlite 並且我之前運行 heroku 日志時的靜態文件沒有問題 --tail 它顯示變量錯誤,因為我使用的是 sqlite 但是我通過設置 postgres 並運行 migrate 解決了這個問題,所以現在一切看起來都很好,所以我被卡住了
日志輸出
2020-09-27T17:17:09.000000+00:00 app[api]: Build succeeded
2020-09-27T17:17:18.450683+00:00 app[web.1]: 10.16.178.50 - - [27/Sep/2020:17:17:18 +0000] "GET / HTTP/1.1" 500 145 "http://zersh.shop/users/seller_reg/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0"
2020-09-27T17:17:18.451441+00:00 heroku[router]: at=info method=GET path="/" host=zersh.shop request_id=c7568fc0-74d7-466b-b309-e9dacb26842f fwd="154.231.163.135" dyno=web.1 connect=0ms service=833ms status=500 bytes=577 protocol=http
urls 項目級別
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import handler404
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('user.urls')),
path('users/', include('django.contrib.auth.urls')),
path('accounts/', include('allauth.urls')),
path('', include('pages.urls')),
path('store/', include('store.urls')),
#path("djangorave/", include("djangorave.urls", namespace="djangorave")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#error handlers
handler404 = 'store.views.error_404'
#handler500 = 'store.views.my_custom_error_view'
設置
import dj_database_url
import dotenv
import django_heroku
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# This is new:
dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
dotenv.load_dotenv(dotenv_file)
DEBUG = False
ALLOWED_HOSTS = ['example.herokuapp.com']
# Application definition
INSTALLED_APPS = [
# my apps
'user.apps.UserConfig',
'store.apps.StoreConfig',
'pages.apps.PagesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.sites',
# 3rd party apps
'crispy_forms',
'allauth',
'allauth.account',
'allauth.socialaccount',
#'djangorave',
#providors
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.google',
]
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 = 'zershecom.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
'store.context_processors.categories_processor',
],
},
},
]
WSGI_APPLICATION = 'zershecom.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {}
DATABASES['default'] = dj_database_url.config(conn_max_age=600)
# Password validation
# https://docs.djangoproject.com/en/3.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/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
AUTH_USER_MODEL = 'user.CustomUser'
#LOGIN_REDIRECT_URL = 'home'
#LOGOUT_REDIRECT_URL = 'home'
# media configurations
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# django-crispy-forms config
CRISPY_TEMPLATE_PACK = 'bootstrap4'
# django-allauth config
LOGIN_REDIRECT_URL = 'home'
ACCOUNT_LOGOUT_REDIRECT = 'home'
ACCOUNT_SESSION_REMEMBER = True
SITE_ID = 1
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_EMAIL_REQUIRED = True
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
},
},
}
django_heroku.settings(locals())
options = DATABASES['default'].get('OPTIONS', {})
options.pop('sslmode', None)
文件
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django = "*"
pillow = "*"
mysqlclient = "*"
django-allauth = "*"
django-crispy-forms = "*"
gunicorn = "*"
psycopg2-binary = "*"
whitenoise = "*"
python-dotenv = "*"
django-heroku = "*"
[requires]
python_version = "3.8"
配置文件
web: gunicorn zershecom.wsgi --log-file -
由於您沒有提供有關您的 requirements.txt 和 procfile 的信息,我相信您可以檢查這些文件並仔細檢查您是否寫出了所有必要的信息。
我認為使用數據庫 URI 將是一個更明智的選擇。 在 heroku 上訪問您的應用程序並獲取數據庫 URI。 https://simpleisbetterthancomplex.com/tutorial/2016/08/09/how-to-deploy-django-applications-on-heroku.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.