I'm making website using Python Framework of 'Django'.
I have some problems with Django about secret_key .
When I set a secret_key in .env file on private and change settings.py, there is problem.
I'm trying to solve it by myself, but its so difficult.
When I runserver on django, It errors contain:
(venv) C:\pragmatic>python manage.py runserver
C:\pragmatic\venv\lib\site-packages\environ\environ.py:637: UserWarning: Error reading C:\
pragmatic\.env - if you're not configuring your environment separately, check this.
warnings.warn(
Traceback (most recent call last):
File "C:\pragmatic\venv\lib\site-packages\environ\environ.py", line 273, in get_value
value = self.ENVIRON[var]
File "C:\Users\PJJ\AppData\Local\Programs\Python\Python38-32\lib\os.py", line 675, in __
getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\pragmatic\venv\lib\site-packages\django\core\management\__init__.py", line 401,
in execute_from_command_line
utility.execute()
File "C:\pragmatic\venv\lib\site-packages\django\core\management\__init__.py", line 395,
in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\pragmatic\venv\lib\site-packages\django\core\management\base.py", line 330, in
run_from_argv
self.execute(*args, **cmd_options)
File "C:\pragmatic\venv\lib\site-packages\django\core\management\commands\runserver.py",
line 61, in execute
super().execute(*args, **options)
File "C:\pragmatic\venv\lib\site-packages\django\core\management\base.py", line 371, in
execute
output = self.handle(*args, **options)
File "C:\pragmatic\venv\lib\site-packages\django\core\management\commands\runserver.py",
line 68, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "C:\pragmatic\venv\lib\site-packages\django\conf\__init__.py", line 83, in __getatt
r__
self._setup(name)
File "C:\pragmatic\venv\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "C:\pragmatic\venv\lib\site-packages\django\conf\__init__.py", line 177, in __init_
_
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\PJJ\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py",
line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\pragmatic\pragmatic\settings.py", line 33, in <module>
SECRET_KEY = env('SECRET_KEY')
File "C:\pragmatic\venv\lib\site-packages\environ\environ.py", line 123, in __call__
return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
File "C:\pragmatic\venv\lib\site-packages\environ\environ.py", line 277, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
settings.py contains:
"""
Django settings for pragmatic project.
Generated by 'django-admin startproject' using Django 3.1.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os, environ
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# reading .env file
environ.Env.read_env(
env_file = os.path.join(BASE_DIR, '.env')
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accountapp',
]
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 = 'pragmatic.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 = 'pragmatic.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/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.1/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.1/howto/static-files/
STATIC_URL = '/static/'
.env contains:
DEBUG=on
SECRET_KEY=o$6-&b%^ww2_+^euxx!=@g7a$j)ef4v(x_z(ojrkhgv=j2b)$_
DATABASE_URL=psql://urser:un-githubbedpassword@127.0.0.1:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
REDIS_URL=rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=ungithubbed-secret
Aucun commentaire:
Enregistrer un commentaire