Django-导入django.conf.settings和导入设置之间的区别-Python 实用宝典

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

Django应用程序中以下import语句之间的基本区别是什么? import settings 和 from django.conf import settings

问题: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


本文由 Python 实用宝典 作者:Python实用宝典 发表,其版权均为 Python 实用宝典 所有,文章内容系作者个人观点,不代表 Python 实用宝典 对观点赞同或支持。如需转载,请注明文章来源。
8

抱歉,评论已关闭!