How to create django application scope variable?

How to create an application area variable loaded when the django application starts, to be in memory and accessible to all.

Basically I want to reuse a variable through an application without reloading it.

thanks

+3
source share
3 answers

You can add it to your settings.pyfile. Or add it to a file __init__.pyinside the application directory.

+7
source

Do you mean something like an environment variable? You can load it through init ...

__init__.py

import os
os.environ['APP_VAR_WHATEVER'] = 'hello world!'
+3
source

Python - ( ), ( ) . Python .

, , settings.py import settings , .

If it is for reading and writing, I will most likely use the database backend, which I already use with Django, instead of the python variable.

If you could provide a more detailed description of what you are trying to achieve, perhaps we could come up with a more suitable solution.

+2
source

Source: https://habr.com/ru/post/1723640/


All Articles