问题:Django-导入django.conf.settings和导入设置之间的区别

Django应用程序中以下import语句之间的基本区别是什么?

import settings

from django.conf import settings

What is the basic difference between the following import statements in a Django app?

import settings

and

from django.conf import settings

回答 0

import settings

将导入Django项目的settings(.py)模块(当然,如果您是从应用程序的“ root”包中编写此代码的话)

from django.conf import settings

将从django.conf包(Django提供的文件)中导入设置对象这很重要,因为

[..]请注意,您的代码不应从global_settings或您自己的设置文件中导入。django.conf.settings抽象了默认设置和特定于站点的设置的概念;它提供了一个界面。它还将使用设置的代码与设置的位置解耦。

更新:如果要定义一些自己的设置,请参阅文档的此部分

import settings

Will import settings(.py) module of your Django project (if you are writing this code from the “root” package of your application, of course)

from django.conf import settings

Will import settings object from django.conf package (Django’s provided files). This is important, because

[..] note that your code should not import from either global_settings or your own settings file. django.conf.settings abstracts the concepts of default settings and site-specific settings; it presents a single interface. It also decouples the code that uses settings from the location of your settings.

UPDATE: if you want to define some own settings, see this part of the documentation


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。