且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

视图之间共享并在AppConfig中初始化的变量

更新时间:2023-02-24 17:53:14

my_config 中的模块级别定义 my_var ,然后将其导入观看次数:

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

请注意 global 关键字.

Note the global keyword.