django-生产和测试环境分离 在settings.py的同级目录下新建settings的文件夹 在settings文件夹下新建 __init__.py base.py develop.py product.py 将settings.py里面的通用部分全部复制到base里面,变动的内容
django-生产和测试环境分离
- 在settings.py的同级目录下新建settings的文件夹
- 在settings文件夹下新建
- __init__.py
- base.py
- develop.py
- product.py
- 将settings.py里面的通用部分全部复制到base里面,变动的内容如: 数据库的配置和DEBUG等除外
- 在develop里面将base里的内容全部导入
-
from .base import *
-
- 将数据库等变动的信息加入即可
- 修改BASE_DIR
-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- BASE_DIR为项目的根目录,此时的根目录相对于该配置文件的上2级目录,就在原来的BASE_DIR的基础上面再加一层os.path.dirname即可
-
- 修改wsgi和manage.py
-
1 import os 2 3 from django.core.wsgi import get_wsgi_application 4 5 profile = os.environ.get(‘PROJECT_PROFILE‘, ‘develop‘) 6 7 os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘ActivityApi.settings.%s‘ % profile) 8 9 application = get_wsgi_application()
wsgi.py
-
1 #!/usr/bin/env python 2 """Django‘s command-line utility for administrative tasks.""" 3 import os 4 import sys 5 6 7 def main(): 8 profile = os.environ.get(‘PROJECT_PROFILE‘, ‘develop‘) 9 os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘ActivityApi.settings.%s‘ % profile) 10 try: 11 from django.core.management import execute_from_command_line 12 except ImportError as exc: 13 raise ImportError( 14 "Couldn‘t import Django. Are you sure it‘s installed and " 15 "available on your PYTHONPATH environment variable? Did you " 16 "forget to activate a virtual environment?" 17 ) from exc 18 execute_from_command_line(sys.argv) 19 20 21 if __name__ == ‘__main__‘: 22 main()
manage.py
-
- 启动
- python manage.py runserver
- pycharm启动
-