What's the difference between `from django.conf import settings` and `import settings` in a Django project

后端 未结 2 1786
一向
一向 2021-01-30 15:43

I\'m reading up that most people do from django.conf import settings but I don\'t undertstand the difference to simply doing import settings in a djang

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 16:17

    import settings will import the first python module named settings.py found in sys.path. Usually (in default django setups) it allows access only to your site defined settings file, which overwrites django default settings (django.conf.global_settings).

    So, if you try to access a valid django setting not specified in your settings file you will get an error.

    django.conf.settings 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 django.conf import settings.

    You can also find it in the django docs.

    Hope this helps.

提交回复
热议问题