django - 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
解决方案
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.
推荐阅读
- node.js - 从参考猫鼬数据库模式查询时未检索数据?
- nginx - 如何在 nginx 中设置具有特定路径片段作为值的 cookie?
- c# - 遇到问题:连接未关闭。连接的当前状态是打开的。- SQL Server & C#
- node.js - 我在哪里将登录的用户数据存储在我的反应应用程序中
- pandas - 熊猫仅在非 nan 时减去列值
- sql - SQL Recursive CTE 在每次递归中替换记录
- mysql - 在 MySQL 5.7 中优化查询
- python - 协调数组切片器
- sip - VOIP/SIP 呼叫保持相关问题
- javascript - @ngrx/data - 撤消乐观删除,UNDO_ONE 操作是否应该恢复 changeState?