将Django部署到PythonAnywhere并且无法在环境变量中使用SECRET_KEY,尽管它已被设置

时间:2017-02-15 05:53:24

标签: python django pythonanywhere

目前,我的错误日志只是吐出了这个错误:

Error running WSGI application 
KeyError: 'SECRET_KEY'
  File "/var/www/www_optranslations_net_wsgi.py", line 24, in <module>
    application = get_wsgi_application() 

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)

  File "/home/optranslations/translation-site/optranslations/settings.py", line 24, in <module>
    SECRET_KEY = os.environ.get('SECRET_KEY')

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None

因此,从我可以收集到的内容来看,它无法找到SECRET_KEY环境变量。 检查交互式python解释器中的bash终端和os.environ命令,正确设置了密钥。

此外,我试过了两个:

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

SECRET_KEY = os.getenv('SECRET_KEY')

在交互式python控制台中使用相同的代码与print进行生成正确的密钥。

根据PythonAnywhere的说明,它在我的wsgi文件中设置如下:

os.environ["SECRET_KEY"] = 'secretkeyhere'

以及我的virtualenv的postactivate文件:

export SECRET_KEY="secretkeyhere"

那么,发生了什么?它只是在我的日志中不断吐出这个错误,我在网上找不到任何东西,甚至开始弄清楚它是什么。不过,我确信这是我忽视的事情。我可以添加您认为必需的代码,所以请告诉我。

修改

有趣的是,现在我得到了这个:

2017-02-15 07:46:27,883 :Error running WSGI application
2017-02-15 07:46:27,884 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2017-02-15 07:46:27,884 :  File "/var/www/www_optranslations_net_wsgi.py", line 24, in <module>
2017-02-15 07:46:27,885 :    application = get_wsgi_application()
2017-02-15 07:46:27,885 :
2017-02-15 07:46:27,885 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2017-02-15 07:46:27,885 :    django.setup(set_prefix=False)
2017-02-15 07:46:27,885 :
2017-02-15 07:46:27,885 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
2017-02-15 07:46:27,885 :    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2017-02-15 07:46:27,885 :
2017-02-15 07:46:27,886 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
2017-02-15 07:46:27,886 :    self._setup(name)
2017-02-15 07:46:27,886 :
2017-02-15 07:46:27,886 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
2017-02-15 07:46:27,886 :    self._wrapped = Settings(settings_module)
2017-02-15 07:46:27,886 :
2017-02-15 07:46:27,886 :  File    "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 116, in __init__
2017-02-15 07:46:27,886 :    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

所以现在它是不正确的配置。但是,我所做的只是停用我的virtualenv并重新加载它以再次激活postactivate脚本。我仍然可以回复$ SECRET_KEY并且看起来很好(并且在交互式控制台中也是getenv)。

我还尝试通过运行以下命令来运行django:

python manage.py shell
from django.conf import settings
print(settings.SECRET_KEY)

当然,它打印出正确的密钥。

1 个答案:

答案 0 :(得分:1)

嗯,答案结果令人尴尬。我的WSGI文件在这些行中有我的os.environ声明:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

他们必须超越这些界限,并且它立即起作用,显而易见的原因现在回顾它。