简体   繁体   English

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

[英]Deploying Django to PythonAnywhere and cannot use SECRET_KEY from environment vars despite it being set

Currently, my error logs are spitting out nothing but this error:目前,我的错误日志除了这个错误之外什么都没有:

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

So, from what I can gather, it can't find the SECRET_KEY environment variable.所以,据我所知,它找不到 SECRET_KEY 环境变量。 Checking from both the bash terminal and the os.environ command in the interactive python interpreter, the key is correctly set in there.从 bash 终端和交互式 python 解释器中的 os.environ 命令检查,密钥在那里正确设置。

Furthermore, I tried both:此外,我尝试了两者:

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

And:和:

SECRET_KEY = os.getenv('SECRET_KEY')

Using the same code to with a print in the interactive python console produces the correct key.在交互式 python 控制台中使用与打印相同的代码生成正确的密钥。

And it is set in my wsgi file as per PythonAnywhere's instructions like so:它按照 PythonAnywhere 的说明在我的 wsgi 文件中设置,如下所示:

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

As well as in my postactivate file for my virtualenv:以及在我的 virtualenv 的 postactivate 文件中:

export SECRET_KEY="secretkeyhere"

So, what's going on?发生什么了? It just keeps spitting out this error in my log and I can't find anything online or even begin to figure out what it is.它只是不断地在我的日志中吐出这个错误,我在网上找不到任何东西,甚至开始弄清楚它是什么。 I'm sure it's something I'm overlooking, though.不过,我确定这是我忽略的东西。 I can add in anymore code you think is required, so please just let me know.我可以添加您认为需要的更多代码,所以请告诉我。

Edit编辑

Interesting, now I'm getting this:有趣的是,现在我得到了这个:

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.")

So now it's ImproperlyConfigured.所以现在它是 ImproperlyConfigured。 However, all I did was deactivate my virtualenv and reload it to activate the postactivate script again.但是,我所做的只是停用我的 virtualenv 并重新加载它以再次激活 postactivate 脚本。 I can still echo $SECRET_KEY and see it fine (and getenv in the interactive console, too).我仍然可以 echo $SECRET_KEY 并看到它很好(以及交互式控制台中的 getenv )。

I also tried to django shell by running the following:我还尝试通过运行以下命令来 django shell:

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

And, of course, it printed out the correct key.而且,当然,它打印出正确的密钥。

Well, the answer turned out to be along embarrassing.好吧,结果证明答案很尴尬。 My WSGI file had my os.environ declarations BELOW these lines:我的 WSGI 文件在这些行下面有我的 os.environ 声明:

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

They had to be above those lines, and it instantly worked, for obvious reasons looking back on it now.他们必须在这些界限之上,而且它立即起作用,现在回想起来的原因显而易见。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当生成的 SECRET_KEY 以“$”开头时,如何从环境文件中转义 Django 中的 SECRET_KEY? - How to escape SECRET_KEY in Django from environment file when generated SECRET_KEY begins with '$'? 使用 appengine “django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量”在 Google 云上部署 Django 应用程序时出错 - Error in deploying Django app on Google cloud using appengine "django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable" windows 中的设置命令不适用于 django SECRET_KEY | django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量 - set command in windows not working for django SECRET_KEY | django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable PythonAnyWhere SECRET_KEY设置不能为空 - PythonAnyWhere The SECRET_KEY setting must not be empty django.core.exceptions.ImproperlyConfigured:设置SECRET_KEY环境变量 - django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable 使用环境变量隐藏 Django SECRET_KEY 的问题 - Issue with Hiding Django SECRET_KEY Using environment variables 环境变量 Django:配置不当:SECRET_KEY 设置不能为空 - Environment Variables Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty VPS 上的 Django SECRET_KEY - Django SECRET_KEY on VPS Django设置SECRET_KEY - Django settings SECRET_KEY 部署Django SECRET_KEY - Deploy Django SECRET_KEY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM