Forums

No SECRET_KEY when start myenv from Web tab

I'd like to ask for another tip if I haven't got exhausted the limit yet.

I can't see SECRET_KEY when start myenv from Web tab. I can see it when run bash console and run myenv by giving "workon myenv" The result is I got error (no SECRET_KEY) when try to run this command in scheduled task command line:

/home/me/.virtualenvs/myenv/bin/python /home/me/project/app/task.py

Unfortunately I got error (no such command) when a try this one in scheduled task command line

workon myenv /home/me/project/app/task.py

I'd really really like to handle with this before summer.

How did you set the SECRET_KEY in your web app and in the scheduled task?

In web app:

1) in /var/www/rafalszymanski_pythonanywhere_com_wsgi.py

import os
import sys
path = '/home/RafalSzymanski/ab'
if path not in sys.path:
    sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'ab.settings.development'
os.environ['SECRET_KEY'] = 'value'

2) in /.virtualenvs/novenv/bin/postactivate

export DJANGO_SETTINGS_MODULE="ab.settings.development"
export SECRET_KEY="value"

In scheduled task - not exactly in scheduled task but in tasks.py file which I want to be executed priodically. I hope these two lines of code are enough to have all settings being set acording to "ab.settings.development".

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ab.settings.development")
django.setup()

In the first version of your scheduled task command, your virtualenv is not being activated, so your postactivate hook will not be called and the environment variable will not be set.

The second version of your scheduled task command won't work at all, since workon doesn't take a command to run as an argument, it just activates the virtualenv.

The solution is to put the os.environ assignment for SECRET_KEY into your tasks.py before you run django.setup()