使用Django堆栈和dotenv在WSGI中查找.env时出现问题

wgxvkvu9  于 12个月前  发布在  Go
关注(0)|答案(2)|浏览(91)

我有一个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路由无效?
任何方向都将受到极大的赞赏。

mmvthczy

mmvthczy1#

我遇到了同样的问题,并通过添加到settings.pywsgi.py解决了这个问题。

import os
from dotenv import load_dotenv
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
load_dotenv(dotenv_path)

字符串
sdarwin建议将路径硬编码为.env
https://github.com/theskumar/python-dotenv/issues/210

tf7tbtn2

tf7tbtn22#

我是这样解决的:

from pathlib import Path

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

try:
    from dotenv import load_dotenv
    load_dotenv(BASE_DIR / '.env')
except:
    print ('Cannot load dotenv variables. Is python-dotenv package installed?')

字符串

相关问题