Reputation: 967
I have this kind of setup :
Now later when I try to get the value of this global variable after the server started, I am unable to see the value. Is this going out of scope? How to store this one time token that is entered before the django starts?
Upvotes: 0
Views: 113
Reputation: 599600
There's no such thing as a "global variable" in Python that is available from everywhere. You need to import all names before you can use them.
Even if you did this, though, it wouldn't actually work on production. Not only is there no command that you run to start up the production server (it's done automatically by Apache or whatever server software you're using), the server usually runs in multiple processes, so the variable would have to be set in each of them.
You should use a setting, as dcrosta suggests.
Upvotes: 2
Reputation: 26258
Judging by the name of the command line option, this sounds like a configuration variable -- why not put it in settings.py
like all the other configurations?
If it's a secure value that you don't want checked in to version control, one pattern I've seen is to put secure or environment-sensitive (i.e. only makes sense in production or development) configurations in a local_settings.py
file which is not checked in to version control, then add to the end of your settings.py
:
try:
from local_settings import *
except ImportError:
# if you require a local_settings to be present,
# you could let this exception rise, or raise a
# more specific exception here
pass
(Note that some people like to invert the import relationship and then use --settings
on the command line with runserver
-- this works, too, but requires that you always remember to use --settings
.)
Upvotes: 2