SoFunction
Updated on 2024-12-16

Django development environment and production environment distinction in detail

Django development environment and production environment setup

In regular Django project development, we often encounter a class of problems, namely: the local development environment and the remote server production environment configuration is not the same. For these differences, the previous practice is to directly modify the configuration of the production environment. But for developers who aspire to automate the system, it is clear that this is extremely stupid.

So what is the regular practice? Since it's a different environment, two different configuration files are a must.

Preparing the configuration file

Prepare two different sets of settings in a new Django project with the same level of directory:

|____EveryDay
| |____prd_settings.py
| |______init__.py
| |____settings.py
| |____dev_settings.py
| |____urls.py
| |____wsgi.py

Importing different configurations from the default setting

I'm not going to modify the default django configuration module here :. Instead, I'm going to differentiate between loading different configurations by identifying whether the current environment is a production environment or a development environment. so, we need to set an environment variable that is only available to production environments, and then determine which configuration to import by determining the existence of this environment variable in the...

show code:

# 
import os

# Default settings between dev and prd

if ('ENV', None):
  from .prd_settings import *
else:
  from .dev_settings import *

Because the server is using CentOS7, we directly add the environment variable in /etc/profile

export ENV="SERVER"

Importing different configurations from the runtime

The -settings option is provided at runtime to specify the settings module, so it's handy for starting the debug server that comes with django.

python  rumserver 0.0.0.0:8000 --settings=EveryDay.prd_settings

This is the entire content of this article.