我有一个Django应用程序,我试图使用dotenv来保护我的SECRET_KEY。manage.py runserver工作正常,但使用apache 2托管的网站无法工作,apache给予我错误日志:
mod_wsgi (pid=32200): Failed to exec Python script file '/opt/bitnami/projects/accuhx/accuhx/wsgi.py'.
mod_wsgi (pid=32200): Exception occurred processing WSGI script '/opt/bitnami/projects/accuhx/accuhx/wsgi.py'.
Traceback (most recent call last):
File "/opt/bitnami/projects/accuhx/accuhx/wsgi.py", line 23, in <module>
application = get_wsgi_application()
File "/opt/bitnami/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/opt/bitnami/python/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
self._setup(name)
File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 196, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
字符串
我假设这是因为我的manage.py可以找到.env,但wsgi.py不能,但我不知道为什么。
我的结构:
accuhx
|
|-manage.py
|-accuhx
| |
| |-settings.py
|-.env
|-wsgi.py
型
WSGI.py:
import os
import dotenv
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'accuhx.settings')
application = get_wsgi_application()
型
manage.py
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'accuhx.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
型
.env
DJANGO_SECRET_KEY=p$********xr
型
settings.py
import os
SECRET_KEY = os.getenv("DJANGO_SECRET_KEY")
型
我按照https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/和https://github.com/theskumar/python-dotenv上的指南操作
我的问题是没有指向wsgi的.env的正确路径吗?为什么它对manage.py runserver有效,而对apache路由无效?
任何方向都将受到极大的赞赏。
2条答案
按热度按时间mmvthczy1#
我遇到了同样的问题,并通过添加到
settings.py
和wsgi.py
解决了这个问题。字符串
sdarwin建议将路径硬编码为.env
https://github.com/theskumar/python-dotenv/issues/210
tf7tbtn22#
我是这样解决的:
字符串