SoFunction
Updated on 2024-12-19

django configuration DJANGO_SETTINGS_MODULE implementation

DJANGO_SETTINGS_MODULE

Use Django to inform Django which configuration file is currently in use. This can be accomplished by changing the environment variable DJANGO_SETTINGS_MODULE.

When we use a local web server, we specify the path to the DJANGO_SETTINGS_MODULE environment variable so that Django knows which configuration file to use.

import os
import sys

if __name__ == "__main__":
    # test1 is my main app name
    ("DJANGO_SETTINGS_MODULE", "")
    try:
        from  import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line()

Django setup and configuration.

The value will be read from the module specified by the DJANGO_SETTINGS_MODULE environment
variable, then from the .global_settings (this is the default setting for django)

Why can't you use import settings?
import settings will import the first python module named found in , usually (in default django setups). It allows access only to your site defined settings file, which overwrites django default settings (.global_settings).So, if you try to access a valid django setting not specified in your settings file you will get an is not a file but a class making an abstraction of the concepts, default settings and your site-specific settings. Django also does other checks when you use from import import settings It's a better choice.

import settings will import the first named python module in sys. Usually (in the default django settings). It only allows access to the site-defined settings file, which overrides django's default settings (.global_settings)。
As a result, attempting to access a valid django setting that is not specified in the settings file will result in an error.
is not a file, but a class that abstracts concepts, default settings, and site-specific settings. When you use from import settings, Django performs other checks as well.
from import settings is a better choice

Django settings

1. Foundation

DJANGO_SETTINGS_MODULE environment variable: let the settings module be included in a directory where python can find it, it is not needed for development cases, we usually run it in the current folder where python can search for it. If you need to run it on another server, you must specify the DJANGO_SETTINGS_MODULE variable.

2. Default settings

A django settings file doesn't require us to manually set up all the items because the system is already set up by default. We only need to modify the settings we use

The default settings are in the django/conf/global_settings.py file. The default settings are in the django/conf/global_settings.py file. django loads the configurations in global_settings.py at compile time, then loads the specified settings file and rewrites the changed settings.

to this article on the django configuration DJANGO_SETTINGS_MODULE article is introduced to this, more related to django configuration DJANGO_SETTINGS_MODULE content please search my previous posts or continue to browse the following articles hope that you will support me in the future more!