"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Manage Django Settings for Local Development and Production?

How to Manage Django Settings for Local Development and Production?

Published on 2024-11-08
Browse:584

How to Manage Django Settings for Local Development and Production?

Separating Local and Production Django Settings

Managing Django settings across local development and production environments can be a challenge. Some settings, such as constants, can be shared between both while others, like file paths, require variation.

The recommended approach, as outlined in "Two Scoops of Django," is to employ version control and store settings in a dedicated directory within your project:

project/
    app1/
    app2/
    project/
        __init__.py
        settings/
            __init__.py
            base.py
            local.py
            production.py
    manage.py

Common Settings in base.py

settings/base.py houses settings shared across both environments, such as MEDIA_ROOT and ADMIN.

Site-Specific Settings

  • settings/local.py contains settings specific to local development, such as DEBUG and development apps.
  • settings/production.py holds settings for the production site, including any necessary app additions.

Executing Django with Specified Settings

When running Django commands, utilize the --settings option to specify the appropriate settings file:

  • Local development: ./manage.py runserver 0:8000 --settings=project.settings.local
  • Production shell: ./manage.py shell --settings=project.settings.production

Sample Project Template

For further guidance, a sample project layout template is available on GitHub.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3