在Settings.py中使用常量 [英] Using Constants in Settings.py

查看:79
本文介绍了在Settings.py中使用常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我的一个模块文件中使用Django项目的 settings.py 中声明的变量吗?

Can I use a variable declared in the Django project's settings.py in one of my module files?

例如,使用 DATABASE_HOST ='databasename'

我试图获取服务器的名称该应用程序目前已部署在您上面。

I'm trying to get the name of the server the application is currently deployed on you see.

推荐答案

事实上,您可以...鼓励。要使用它,请从django.conf导入设置(这将导入您的项目的设置):

You certainly can... it's encouraged, in fact. To use it, import the settings from django.conf (this imports your project's settings):

from django.conf import settings
print "My database host is %s" % settings.DATABASE_HOST

使用Python代码中的设置解释为什么这样工作,为什么这比直接导入 settings.py 模块更好。

The documentation on Using settings in Python code explains why this works, and why this is preferable over importing the the settings.py module directly.

这篇关于在Settings.py中使用常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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