The cause of

When developing a Django project, I often encounter situations where the production environment and the development environment need to use different Settings files. In the past, I used to upload the Settings files to the server after the development environment and then modify them on the server. In this article, you can use Git branches to switch Settings files between different environments. In this article, you can use Git branches to switch Settings files between different environments. Maybe I don’t know Git very well, but this article still provides me with a good idea. Then I came across another article on Google about the difference between A Django development environment and a production environment, and combined those two articles to customize the following solution.

plan

Use Python’s os. enviro. get method to obtain the environment variables of the current environment. Import different environment variables from the Settings file as follows:

Split setup file

Create a Settings directory in Django’s project directory, and then create Settings files such as base.py, dev.py, pro.py, and test.py in your Settings directory. Py, dev. Py, pro.py. They represent base Settings, development environment Settings, and production environment Settings respectively.

Set the entry to the split Settings file

Base. Py is generally used as the split Settings file entry

.Import different Settings files based on environment variables
Import production environment Settings if ENV is present in the environment variable
Otherwise, import development environment Settings for development environment
if os.environ.get('ENV'.None) :from .pro import *
else:
    from .dev import*...Copy the code

Modify related files

After splitting the Settings file, modify the files that reference the Settings file: wsgi.py, manage.py

# wsgi.py
Modify this file to ensure that the program finds the Settings file correctly when it launches via UWSGi
import os
from django.core.wsgi import get_wsgi_application

Add the environment variable DJANGO_SETTINGS_MODULE to backend.settings.bases
# The specific value should be set according to the file entry path of the individual project
os.environ['DJANGO_SETTINGS_MODULE'] = 'backend.settings.base'

application = get_wsgi_application()
Copy the code
# manage.py
Modify this file to ensure that the program finds the Settings file correctly when it starts with Python manage.py runserver

#! /usr/bin/env python
import os
import sys

if __name__ == '__main__':
    Add the environment variable DJANGO_SETTINGS_MODULE to backend.settings.bases
    # The specific value should be set according to the file entry path of the individual project
    os.environ['DJANGO_SETTINGS_MODULE'] = 'backend.settings.base'
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)
Copy the code

Production Environment Configuration

Finally, just set the environment variable that matches the entry file in the production environment:

# vim /etc/profile
# add to the last line:
export ENV="SERVER"
You can set whatever value you want but it must match the entry file
Copy the code

Begin to use

After finishing the above configuration, you can start to use it, develop and produce two sets of configuration, and there is no need to modify the file after uploading.

conclusion

The two core points of the whole process can be seen from the scheme:

  1. Split setup file
  2. Find one that can get throughPythonPoints that distinguish between different environments

As long as you can accomplish the above two points, you can customize your own solution.

reference

EveryDay: Django engineering structures and production environment switches using Git branches

SmartKeyerror: Distinguishing between a Django development environment and a production environment