gpt4 book ai didi

PythonAnyWhere SECRET_KEY 设置不能为空

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:36 28 4
gpt4 key购买 nike

所以我想在 pythonanywhere 上部署我的网站。我创建了 virtualenv,安装了 Django 和我的网站所需的所有应用程序。我创建了新应用程序并将其与我从 GitHub 克隆的项目连接起来。这是我的网络应用程序中设置的屏幕截图:

enter image description here

我的项目结构是这样的:

enter image description here

我的设置.py

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__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY')

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'taggit',
'blog',
'widget_tweaks',
'ckeditor',
'ckeditor_uploader',
'user_account',
'captcha',
'mptt',
]

SITE_ID = 1

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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 = 'blog_project.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',
],
},
},
]

WSGI_APPLICATION = 'blog_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

"""
# Use this for testing on local pc
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
"""

#Use this for Deployment
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'falca94$blogezzdatabase',
'USER': 'falca94',
'PASSWORD': os.environ.get('DATABASE_PASS'),
'HOST': 'falca94.mysql.pythonanywhere-services.com',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 5,
},
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Django-allauth
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',

# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/Belgrade'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
#'/var/www/static/',
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media_cdn')

#slanje maila
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'blogezz.master@gmail.com'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# Custom allauth settings
# Use email as the primary identifier
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
# Make email verification mandatory to avoid junk email accounts
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
# Eliminate need to provide username, as it's a very old practice
ACCOUNT_USERNAME_REQUIRED = False
# Redirect user after logout
ACCOUNT_LOGOUT_REDIRECT_URL = "/accounts/login"
# Our personal signup form
ACCOUNT_SIGNUP_FORM_CLASS = 'user_account.forms.SignupForm'

ACCOUNT_ADAPTER = 'user_account.adapter.AccountAdapter'

# To not show the logout page after the user logs out, set ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_LOGOUT_ON_GET = False

# Configuration for DJANGO-CKEDITOR
CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_RESTRICT_BY_USER = True
CKEDITOR_BROWSE_SHOW_DIRS = True
CKEDITOR_RESTRICT_BY_DATE = True
CKEDITOR_JQUERY_URL = '//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'

CKEDITOR_CONFIGS = {
'default': {
'skin': 'moono',
'height': 800,
},
}

还有我的/var/www/falca94_pythonanywhere_com_wsgi.py

# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/falca94/mysite/mysite/settings.py'
## and your manage.py is is at '/home/falca94/mysite/manage.py'
path = '/home/falca94/blog_project'
if path not in sys.path:
sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'blog_project.settings'
os.environ['SECRET_KEY'] = 'mysecretkey'
os.environ['EMAIL_PASS'] = 'emailpassword'
os.environ['DATABASE_PASS'] = 'databasepass'

# then, for django >=1.5:
from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())
## or, for older django <=1.4
#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()

我不明白为什么会出现此错误:

Error running WSGI application
2017-05-05 11:33:43,246 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2017-05-05 11:33:43,246 : File "/var/www/falca94_pythonanywhere_com_wsgi.py", line 12, in <module>
2017-05-05 11:33:43,247 : application = StaticFilesHandler(get_wsgi_application())
2017-05-05 11:33:43,247 :
2017-05-05 11:33:43,247 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2017-05-05 11:33:43,247 : django.setup(set_prefix=False)
2017-05-05 11:33:43,247 :
2017-05-05 11:33:43,247 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/__init__.py", line 22, in setup
2017-05-05 11:33:43,247 : configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2017-05-05 11:33:43,247 :
2017-05-05 11:33:43,248 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
2017-05-05 11:33:43,248 : self._setup(name)
2017-05-05 11:33:43,248 :
2017-05-05 11:33:43,248 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
2017-05-05 11:33:43,248 : self._wrapped = Settings(settings_module)
2017-05-05 11:33:43,248 :
2017-05-05 11:33:43,248 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/conf/__init__.py", line 129, in __init__
2017-05-05 11:33:43,248 : raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

最佳答案

您的 settings.py 尝试从环境变量中读取 SECRET_KEY:

SECRET_KEY = os.environ.get('SECRET_KEY')

错误表明您尝试运行 Django 应用程序的环境中未设置此环境变量。

就 Python Anywhere 而言,您可以在其文档中阅读有关此案例的信息:https://help.pythonanywhere.com/pages/environment-variables-for-web-apps

关于PythonAnyWhere SECRET_KEY 设置不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43805957/

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