The original address: www.cloudcrossing.xyz/post/27/
When submitting your blog code to the repository, you may inadvertently expose information (database urls, passwords, keys, debug status, email hosts, permitted hosts, etc.) that you don’t want to divulge. So we need to think about keeping our information in a safe place (like directly on the server instead of Github).
(95) To do this, we will use a module, Python-Provocative, that can isolate Settings in code and save them in a file (which can be stored directly on the server rather than in the repository), called an environment file.
# # # to install
() Execute the PIP install python-trigger command
# # # usage
When you open settings.py, you’ll typically see code like this:
SECRET_KEY = '3izb^ryglj(bvrjb2_y1fZvcnbky#358_l6-nn#i8fkug4mmz! ' DEBUG = True DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'HELLO_DJANGO', 'USER': 'U_HELLO', 'PASSWORD': 'hA8(scA@! Fg3 * sc&xaGh & 6%. -l < _ & xCf ', 'HOST' : '127.0.0.1' and 'PORT' : ', '}}Copy the code
Env (or.ini, for details) in the project root directory (the same directory as manage.py) and save the following
SECRET_KEY=3izb^ryglj(bvrjb2_y1fZvcnbky#358_l6-nn#i8fkug4mmz! DEBUG=True DB_NAME=HELLO_DJANGO DB_USER=U_HELLO DB_PASSWORD=hA8(scA@! Fg3 * sc&xaGh & -l < 6%. _ & xCf DB_HOST = 127.0.0.1Copy the code
Remember to add.env to.gitignore to ensure that environment files are not committed to the repository.
To modify settings.py, import the module first
() import OS # from () import config BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = config('SECRET_KEY') DEBUG = config('DEBUG', cast=bool) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': config('DB_NAME'), 'USER': config('DB_USER'), 'PASSWORD': config('DB_PASSWORD'), 'HOST': config('DB_HOST'), 'PORT': "',}}Copy the code
#### Transfer data
Let’s start with two lines of code
DEBUG = config('DEBUG', cast=bool)
EMAIL_PORT = config('EMAIL_PORT', cast=int)Copy the code
The config() function takes two arguments, the name of the variable passed the value and the type of the value passed. Normally, the data we read from a file is saved as a string, and this is where the cast parameter comes in handy. In the function config(), the cast argument can specify the type of data to read. Looking back at the first line above, the value of DEBUG read from the environment file is specified as a Boolean value.
For ALLOWED_HOSTS
ALLOWED_HOSTS=.localhost, .herokuapp.comCopy the code
We can specify its cast parameter as
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=lambda v: [s.strip() for s in v.split(',')])Copy the code
As complicated as this looks, we can use the Csv function that comes with this module.
from decouple import config, Csv
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv())Copy the code
# # # # the default values
If there is a value defined in the environment file (that is, it is not in the environment file), we can pass config() an extra parameter to specify the default value.
DEBUG = config('DEBUG', default=True, cast=bool)Copy the code
# # # #
Sometimes you don’t want to change the parameters in the environment file, you can override the variables in the environment file. Because environment variables (os.environ) take precedence over environment file variables: DEBUG=True python manage.py
(note that Goldman always assigns values in this order:
- Environmental variables;
- Environment File:
.in
I or.env
File; - Default parameters passed to the configuration.
Upload the environment file to the server
Here we mainly use xshell, LRZSZ. First use Xshell to connect to the server.
Yum install -y LRZSZ yum install -y LRZSZ yum install -y LRZSZ yum install -y LRZSZ yum install -y LRZSZ
To download the server file, run sz filename.