Django 会话在本地正常工作,但一旦部署到 Heroku,它们就会在每次视图更改时刷新
Django session working normally locally but once deployed to Heroku they are refreshed upon every view change
会话在本地工作时在更改视图时完美运行,但是当部署到 Heroku 时,就好像会话被刷新并且它包含的所有信息在每次视图更改时都被删除。我正在使用 Heroku 的 Postgres 数据库。
我已经看过:Django Session Not Working on Heroku 但问题仍然存在。其他人也有同样的问题,但没有明确的答案。
这是我当前的设置文件。任何帮助将不胜感激
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'e488a0185303170daa47fe1de243823fbb3db60f045e6eae'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'here goes the heroku host']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'....',
'....',
]
ASGI_APPLICATION = 'System.asgi.application'
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 = 'System.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
WSGI_APPLICATION = 'System.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
SESSION_ENGINE= 'django.contrib.sessions.backends.cached_db'
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {'min_length': 8}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'authentication.validators.NumericValidator',
},
{
'NAME': 'authentication.validators.UppercaseValidator',
},
{
'NAME': 'authentication.validators.LowercaseValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = False
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'authentication/media/')
MEDIA_URL = '/authentication/media/'
您正在将会话保存到您的数据库,这在 Heroku 上是一个合理的选择,但您正在使用 SQLite 作为您的数据库。
Heroku 的 ephemeral filesystem makes SQLite a poor choice of database. Data will not be shared among dynos and will be lost whenever your dyno restarts. This happens frequently(每天至少一次)。
无论您选择如何处理会话,如果您想继续使用 Heroku,都应该从 SQLite 迁移到像 PostgreSQL 这样的 client-server 数据库。简单地这样做可能会作为副作用解决您的会话问题。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '...db name',
'USER': "postgres",
'PASSWORD': '...db password',
'HOST': 'localhost',
'PORT': '5432',
} }
会话在本地工作时在更改视图时完美运行,但是当部署到 Heroku 时,就好像会话被刷新并且它包含的所有信息在每次视图更改时都被删除。我正在使用 Heroku 的 Postgres 数据库。
我已经看过:Django Session Not Working on Heroku 但问题仍然存在。其他人也有同样的问题,但没有明确的答案。
这是我当前的设置文件。任何帮助将不胜感激
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'e488a0185303170daa47fe1de243823fbb3db60f045e6eae'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'here goes the heroku host']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'....',
'....',
]
ASGI_APPLICATION = 'System.asgi.application'
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 = 'System.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
WSGI_APPLICATION = 'System.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
SESSION_ENGINE= 'django.contrib.sessions.backends.cached_db'
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {'min_length': 8}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'authentication.validators.NumericValidator',
},
{
'NAME': 'authentication.validators.UppercaseValidator',
},
{
'NAME': 'authentication.validators.LowercaseValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = False
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'authentication/media/')
MEDIA_URL = '/authentication/media/'
您正在将会话保存到您的数据库,这在 Heroku 上是一个合理的选择,但您正在使用 SQLite 作为您的数据库。
Heroku 的 ephemeral filesystem makes SQLite a poor choice of database. Data will not be shared among dynos and will be lost whenever your dyno restarts. This happens frequently(每天至少一次)。
无论您选择如何处理会话,如果您想继续使用 Heroku,都应该从 SQLite 迁移到像 PostgreSQL 这样的 client-server 数据库。简单地这样做可能会作为副作用解决您的会话问题。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '...db name',
'USER': "postgres",
'PASSWORD': '...db password',
'HOST': 'localhost',
'PORT': '5432',
} }