How to properly runserver on different settings for Django?

前端 未结 7 872
悲哀的现实
悲哀的现实 2021-02-07 14:42

I have a basic django rest API. I want to separate some of my settings for dev and prod just for organizational purposes. I\'m also just learning about separating environments.

相关标签:
7条回答
  • 2021-02-07 15:46

    For example I open SQL log in dev settings

    1. My file structure:
      create settings folder and mv original settings.py to settings/defaults.py

    2. load defaults in init.py

    # proj/proj/settings/__init__.py
    
    from .defaults import *
    
    1. Edit dev.py
    # proj/proj/settings/dev.py
    
    from .defaults import *
    DEBUG = True
    
    # print sql to the console
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
            }
        },
        'loggers': {
            'django.db.backends': {
                'handlers': ['console'],
                'propagate': True,
                'level': 'DEBUG',
            }
        },
    }
    
    
    1. run in the python env
    ./manage.py runserver --settings proj.settings.dev
    

    Django 2.1.7 | Mac

    0 讨论(0)
提交回复
热议问题