配置文件區分環境原因:
不同環境比如開發環境,測試環境、生產環境等,配置文件時不一樣的,為了開發和部署的方便,將不同環境的配置放在不同的文件中,便於管理
Django項目中可以在項目中定義一個專門文件夾用於存放配置文件:
說明:
- 上面圖片中的settings包用於存放配置文件
- dev.py為開發環境配置文件,prod.py為生產環境配置文件
修改啟動后加載的配置文件
1.開發環境,修改項目下的manage.py:
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings.dev") # 這里指定開發環境的配置文件:settings文件夾下的dev try: from django.core.management import execute_from_command_line except ImportError: # The above import may fail for some other reason. Ensure that the # issue is really that Django is missing to avoid masking other # exceptions on Python 2. try: import django except ImportError: 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?" ) raise execute_from_command_line(sys.argv)
2.生產環境:修改whgi.py中的配置文件
""" WSGI config for meiduo_mall project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings.prod") # 生產環境的配置文件:settings.prod application = get_wsgi_application()
3.獲取配置文件中的參數值:
from django.conf import settings # 獲取配置文件中的 EMAIL_FROM 值 ef = settings.EMAIL_FROM