How to properly runserver on different settings for Django?

前端 未结 7 861
悲哀的现实
悲哀的现实 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:45

    Create a folder called config

    config/
        commonsettings.py
        dev.py
        prod.py
    

    make sure that in dev.py and prod.py you import everything from commonsettings.py like this:

    from .commonsettings import * 
    

    then if you want to run the dev.py settings:

    python manage.py runserver --settings=config.dev
    

    if you want to run prod.py:

    python manage.py runserver --settings=config.prod
    

    NOTE:

    For more readable files many developers call their settings files: local.py (for the local settings) production.py (for the production settings) and base.py (the common settings for both)

    Personally I place my setting files in:

    config/
        settings/
            base.py
            local.py
            production.py
            test.py (For tests)
    

提交回复
热议问题