将Django部署到PythonAnywhere,并且在环境vars中不能使用SECRET_KEY,尽管它已经被设置

时间:2021-06-10 22:42:30

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. Checking from both the bash terminal and the os.environ command in the interactive python interpreter, the key is correctly set in there.

因此,根据我的收集,它无法找到SECRET_KEY环境变量。检查bash终端和os。环境在交互式python解释器中,关键是正确设置在那里。

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:

它在我的wsgi文件中是按照PythonAnywhere的指示设置的:

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

As well as in my postactivate file for my virtualenv:

以及在我的后激活文件为我的虚拟机:

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. However, all I did was deactivate my virtualenv and reload it to activate the postactivate script again. I can still echo $SECRET_KEY and see it fine (and getenv in the interactive console, too).

所以现在ImproperlyConfigured。然而,我所做的只是停用我的virtualenv并重新加载它以再次激活postactivate脚本。我仍然可以回显$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.

当然,它打印出了正确的键。

1 个解决方案

#1


1  

Well, the answer turned out to be along embarrassing. My WSGI file had my os.environ declarations BELOW these lines:

答案是令人尴尬的。我的WSGI文件有我的os。下列各行环境声明:

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.

他们必须在这些线之上,而且它立即起作用,原因很明显,现在回想起来。

#1


1  

Well, the answer turned out to be along embarrassing. My WSGI file had my os.environ declarations BELOW these lines:

答案是令人尴尬的。我的WSGI文件有我的os。下列各行环境声明:

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.

他们必须在这些线之上,而且它立即起作用,原因很明显,现在回想起来。