开发者

Using Constants in Settings.py

开发者 https://www.devze.com 2022-12-13 15:27 出处:网络
Can I use a variable declared in the Django project\'s settings.py in one of my module files?开发者_运维技巧

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

开发者_运维技巧

For instance, using DATABASE_HOST = 'databasename'?

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


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

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.


yes

from django.conf import settings

print settings.MY_SETTINGS_VAR
0

精彩评论

暂无评论...
验证码 换一张
取 消