首页 > 解决方案 > Django 设置.py

问题描述

在我的项目登录时,加载了一些 settings.py 环境变量以启用一些行为:

unit_id = settings.COMPANY

当另一个登录系统的用户更改此变量的值时,通过一个函数,它会反映在所有其他已经处于活动状态的用户中:

settings.COMPANY = "coke"

在这种情况下,所有用户都会在 settings.COMPANY 中看到“可乐”。我相信这会在内存中,并且只适用于有问题的用户部分,因为我没有写在物理文件中。

我想知道这是否是 Django 处理 settings.py 环境变量的方式:它是否动态传播到所有用户打开的所有实例?

此变量由 context_processors.py 访问,如下所示:


def units(request):
    unit_id = settings.COMPANY

标签: djangosettings

解决方案


You should not change settings at runtime.

This is (mainly) because Django doesn't know anything about it's runtime, so it's definitely possible to run multiple instances of the same Django installation. Changing a setting like this will not propagate it to any other processes.

I wonder if this is how Django handles the settings.py environment variables: Does it propagate dynamically to all instances opened by all users?

Django doesn't run an instance for every user. There is one or more (if you for example use something like gunicorn or if you use multiple servers with a load balancer.) processes that listen on a certain port.

To have some changeable setting, you could specify a default value, but you should store something like the active company in the database.


推荐阅读