问题
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