尝试在 PythonAnywhere 中查看我的站点时出错

Error when trying to see my site in PythonAnywhere

我已将我的站点放在 PythonAnywhere 中,但是当我尝试通过浏览器查看它时,它生成了一个错误。它与密钥有关。在项目中(创建于pycharm),密钥存储在.env文件中,而在PythonAnywhere中,密钥存储在.profile文件中的环境变量中。

.profile 文件现在是这样的:

# Ensures bash settings are also loaded when using SSH access
source ~/.bashrc
export SECRET_KEY='django_secret_key'

这是日志错误:

2022-05-01 20:55:38,286: Error running WSGI application
2022-05-01 20:55:38,303: decouple.UndefinedValueError: SECRET_KEY not found. Declare it as envvar or define a default value.
2022-05-01 20:55:38,303:   File "/var/www/ezaublog_pythonanywhere_com_wsgi.py", line 15, in <module>
2022-05-01 20:55:38,303:     application = get_wsgi_application()
2022-05-01 20:55:38,304: 
2022-05-01 20:55:38,304:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2022-05-01 20:55:38,304:     django.setup(set_prefix=False)
2022-05-01 20:55:38,304: 
2022-05-01 20:55:38,304:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
2022-05-01 20:55:38,305:     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2022-05-01 20:55:38,305: 
2022-05-01 20:55:38,305:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
2022-05-01 20:55:38,305:     self._setup(name)
2022-05-01 20:55:38,305: 
2022-05-01 20:55:38,305:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
2022-05-01 20:55:38,305:     self._wrapped = Settings(settings_module)
2022-05-01 20:55:38,305: 
2022-05-01 20:55:38,306:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
2022-05-01 20:55:38,306:     mod = importlib.import_module(self.SETTINGS_MODULE)
2022-05-01 20:55:38,306: 
2022-05-01 20:55:38,306:   File "/home/ezaublog/ezaublog.pythonanywhere.com/mysite/settings.py", line 25, in <module>
2022-05-01 20:55:38,306:     SECRET_KEY = config('SECRET_KEY')
2022-05-01 20:55:38,306: 
2022-05-01 20:55:38,306:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/decouple.py", line 243, in __call__
2022-05-01 20:55:38,306:     return self.config(*args, **kwargs)
2022-05-01 20:55:38,306: 
2022-05-01 20:55:38,306:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/decouple.py", line 105, in __call__
2022-05-01 20:55:38,307:     return self.get(*args, **kwargs)
2022-05-01 20:55:38,307: 
2022-05-01 20:55:38,307:   File "/home/ezaublog/.virtualenvs/ezaublog.pythonanywhere.com/lib/python3.8/site-packages/decouple.py", line 90, in get
2022-05-01 20:55:38,307:     raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
2022-05-01 20:55:38,307: ***************************************************
2022-05-01 20:55:38,307: If you're seeing an import error and don't know why,
2022-05-01 20:55:38,307: we have a dedicated help page to help you debug: 
2022-05-01 20:55:38,307: https://help.pythonanywhere.com/pages/DebuggingImportError/
2022-05-01 20:55:38,308: ***************************************************

这是我的 setting.py 文件的一部分。它显示了如何从环境变量中读取 SECRET_KEY:

from pathlib import Path
from decouple import config


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')

通过日志可以看出我使用的是python-decouple包。当我在主机 (PythonAnywhere) 中打开一个终端时,我可以通过 printenv variable_nameecho $variable_name 命令看到存储密钥的环境变量。即便如此,django 似乎看不到恢复密钥的环境变量。

注意:我没有触及 wsgi.py 文件。

我该如何解决这个问题?

我解决了。问题是 Django 没有看到存储密钥的环境变量(你可以很容易地在错误日志中看到我 post 在问题中编辑)。我需要做的就是在主机(PythonAnywhere)的项目目录中创建一个 .env 文件来存储密钥,然后站点开始工作。

@SarahMesser post在 PythonAnywhere 帮助页面的 link 评论中编辑了演示如何操作的内容。值得一提的是,我唯一需要做的就是创建这个 .env 文件。我不必更改代码即可使用帮助页面中提到的 python-dotenv 包(我仍然使用 python-decouple ,它起作用了)也不使用 postactivate 脚本。

尽管网站现在可以正常运行,但我不明白为什么 Django 看不到我在 .profile 文件中创建的环境变量,但这可能是 post 之外的问题'范围。