Under normal circumstances, Django will help us create a settings.py directory, and all configuration files will be placed in this directory.
However, how to better distinguish the development environment from the generation environment in the same configuration and modify the configuration every time it is deployed is obviously not a rational behavior.
The use of conditions such as if... Else... To distinguish assignments will make settings particularly bloated and increase the difficulty of debugging.
So how to solve this problem?
Obviously, splitting settings is a good choice.
Next, let's see how to split settings.
Step 1: create a Python package named settings in the project directory, and then create three Python files: base.py, development.py and product.py, which are basic environment settings, development environment settings and production environment settings respectively.
Step 2: copy the contents of the text settings.py to base.py for backup, then delete the original settings.py, and then modify product.py to generate the environment configuration file and the development.py development environment configuration file according to your needs.
Step 3: modify the manage.py and wsgi.py files.
Original text:
import os import sys def main(): os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'InSar_project.settings') 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) if __name__ == '__main__': main()
Amend to read:
import os import sys def main(): profile = os.environ.get('PROJECT_PROFILE', 'develop') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_project.settings.%s' % profile) 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) if __name__ == '__main__': main()
Similarly, modify the wsgi.py file, and then put the contents of different configuration files into the corresponding directory file.
It can be seen that you can use different configuration files only by modifying the environment variables in os.environ.
That's a new problem. Wouldn't it be troublesome to set the environment variable in os.environ every time you start.
Can I directly tell the system which environment setting file I want to start in the startup instruction of Django (Python manage.py runserver)?
Certainly. Continue to modify the code of manage.py!
import os import sys def main(): if 'develop' in sys.argv: os.environ['PROJECT_PROFILE'] = 'develop' sys.argv.remove('develop') elif 'product' in sys.argv: os.environ['PROJECT_PROFILE'] = 'product' sys.argv.remove('product') else: pass profile = os.environ.get('PROJECT_PROFILE', 'base') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'InSar_project.settings.%s' % profile) 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) if __name__ == '__main__': main()
In fact, the instructions you enter will be divided into a list of strings according to the spaces and placed in sys.argv.
as ['manage.py', 'runserver', '192.168.2.140:8002'].
At this time, we only need to judge the string in the list, and then assign a value to the environment variable. We can use the instruction to specify which configuration file to start.
After I execute: python manage.py runserver product 192.168.2.140:8002, the effect diagram is as follows: