2
我正在通過the TaskBuster Django教程工作,其目標是幫助爲項目建立良好的開發設置過程。SECRET_KEY錯誤與環境變量
當我運行命令 回聲$ SECRET_KEY
在任何我的「開發」環境或我的「測試」環境我得到的結果相同,所以我相信我的$ ENVIROMENTS /斌/ postactivate和predeativate變量正確設置。
我base.py文件夾包含
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
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.8/howto/deployment/checklist/
# Get SECRET_KEY from the virtual environment
from django.core.exceptions import ImproperlyConfigured
def get_env_variable(var_name):
try:
return os.environ[var_name]
except KeyError:
error_msg = "Set the %s environment variable" % var_name
raise ImproperlyConfigured(error_msg)
SECRET_KEY = get_env_variable('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'bapsite.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 = 'bapsite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
然而,當我嘗試運行我得到這個輸出的測試。
Traceback (most recent call last):
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/base.py", line 28, in get_env_variable
return os.environ[var_name]
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/os.py", line 725, 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 10, in <module>
execute_from_command_line(sys.argv)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/base.py", line 378, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/base.py", line 351, in create_parser
self.add_arguments(parser)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/commands/test.py", line 52, in add_arguments
test_runner_class = get_runner(settings, self.test_runner)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/test/utils.py", line 144, in get_runner
test_runner_class = settings.TEST_RUNNER
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/testing.py", line 2, in <module>
from .base import *
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/base.py", line 33, in <module>
SECRET_KEY = get_env_variable('SECRET_KEY')
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/base.py", line 31, in get_env_variable
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
這使我相信,有一些問題與祕密密鑰,但我「米不清楚應該怎麼解決。
是os.environ [VAR_NAME]返回錯誤值的回報?如何看什麼正在恢復,所以我可以將它指向什麼,我寧願它返回?我是正確的道路上搞清楚了這一點?
這裏是我使用當前
測試文件from selenium import webdriver
from django.core.urlresolvers import reverse
from django.contrib.staticfiles.testing import LiveServerTestCase
class HomeNewVisitorTest(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def get_full_url(self, namespace):
return self.live_server_url + reverse(namespace)
def test_home_title(self):
self.browser.get(self.get_full_url("home"))
self.assertIn("TaskBuster", self.browser.title)
def test_h1_css(self):
self.browser.get(self.get_full_url("home"))
h1 = self.browser.find_element_by_tag_name("h1")
self.assertEqual(h1.value_of_css_property("color"),
"rgba(200, 50, 255, 1)")
感謝您的回答。我是否誤解了$ echo $ SECRET_KEY返回的命令?我認爲這是我postactivate文件中的祕密密鑰。 – aisflat439
不,'echo $ SECRET_KEY'事實上會返回環境變量。如果我沒有記錯的話,taskbuster教程會告訴你爲這些環境設置不同的Python環境和不同的設置文件。確保在測試環境中使用'export DJANGO_SETTINGS_MODULE =「taskbuster.settings.testing」設置環境變量DJANGO_SETTINGS_MODULE。你可能只需要在教程中稍微回溯一下。 – Andreas
我去檢查我的_test/bin/postactivate_文件,發現我犯的錯誤。安德烈亞斯和帕特里克都在正確的地方。我設置了SECRET_KEY變量,但忽略**導出**,所以我可以回顯它,但其他程序無法訪問它。謝謝你的幫助!這個問題很難解決,因爲我在_dev/bin/postactivate_中正確地完成了它。 – aisflat439