Django项目配置不同环境的配置文件
配置文件区分环境原因:
不同环境比如开发环境,测试环境、生产环境等,配置文件时不一样的,为了开发和部署的方便,将不同环境的配置放在不同的文件中,便于管理
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