Linux adds environment variables and Django automatically switches configuration file versions


A headache these two days is that when using south for database migration, it is found that South will have bugs when dealing with user-defined user models (this problem will be discussed later). When using the online method, we always find that the database can’t write data at all. Today, we found the reason. It turned out that I SSH to the server directly, and Django on the server uses the file. In this case, we must specify the configuration file when we operate, such aspython syncdb --settings=***.server_settingsBut I forgot this. As a result, the local configuration file is used and the SQLite database is written..
To avoid this problem, I thought of a way to let Django choose the configuration file by itself. First, set a custom environment variable on the server. The other is a temporary environment variable. It disappears after the shell is turned off,

$ export HELLO="Hello!"
$ echo $HELLO

The other is permanent, that is, edit the / ECT / profile file and append it at the end

export ENV

Then write this in

import os
env = os.environ.get("ENV", None)
if env == "serrver":
    from .server_settings import *
    from .local_settings import *

That’s good