Django - 导入django.conf.settings和导入设置之间的区别 [英] Django - Difference between import django.conf.settings and import settings

查看:175
本文介绍了Django - 导入django.conf.settings和导入设置之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 导入设置

  django.conf导入设置


解决方案

 导入设置

将导入Django项目的设置(.py)模块(如果你是从您的应用程序的根包中编写此代码,当然)

 从django.conf导入设置
将从django.conf包(Django提供的文件)导入设置对象

这很重要,因为


[..]请注意,您的代码不应该从global_settings或您自己的设置文件导入。 django.conf.settings摘录了默认设置和站点特定设置的概念;它提供了一个单一的接口。它还会将使用设置的代码与设置的位置分离。


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


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

import settings

and

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

这篇关于Django - 导入django.conf.settings和导入设置之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆