How to use django_crontab with os.environ['SECRET_KEY']?

為{幸葍}努か 提交于 2021-02-11 12:50:07

问题


I'm using a django_crontab library and it worked fine until I decided to export my SECRET_KEY to an environmental variable. Summing up what I have done in bash (while in my venv):

export SECRET_KEY='some_secret_key_from_settings'

In settings.py:

SECRET_KEY = os.environ['SECRET_KEY']

In addition, I use venv, so I've also added this to settings:

CRONTAB_PYTHON_EXECUTABLE = '/path_to_my_venv/venv/bin/python3'

This is the error that I have:

SECRET_KEY = os.environ['SECRET_KEY']
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'SECRET_KEY'

The closest solution I found was this: https://github.com/kraiz/django-crontab/issues/74

However, I'm not too experienced in this stuff and didn't understand what I should do. I will be very grateful for answers with explicit steps I could take to make it work


回答1:


If using environment variables isn't a MUST for you, you can use python-decouple package which is pretty straightforward and easy to use. see this at pypi.

then, you can read SECRET_KEY like this: (in settings.py)

    ...
    from decouple import config
    ...

    SECRET_KEY = config('SECRET_KEY')

should solve your problem.



来源:https://stackoverflow.com/questions/66012924/how-to-use-django-crontab-with-os-environsecret-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!