I want to have a variable which I initialize as the application startup and to which I get get access from the views:
# my_app/my_config.py
class WebConfig(AppConfig):
name = '....'
verbose_name = '....'
def ready(self):
print('loading...')
warnings.filterwarnings("ignore")
my_var = {}
# my_app/views.py
def index(request):
# my_var isn't accessible
I can't store my_var
into the session because the session isn't available at WebConfig.
How can I do that then?
Define the my_var
at the module level in the my_config
and then import it in the views
:
my_app/my_config.py
my_var = None
class WebConfig(AppConfig):
...
def ready(self):
global my_var
my_var = {}
my_app/views.py
from my_app.my_config import my_var
def index(request):
print my_var
Note the global
keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With