Federer
Federer

Reputation: 34775

Using Constants in Settings.py

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.

Upvotes: 2

Views: 3131

Answers (2)

user187676
user187676

Reputation:

yes

from django.conf import settings

print settings.MY_SETTINGS_VAR

Upvotes: 3

Jarret Hardie
Jarret Hardie

Reputation: 98002

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.

Upvotes: 16

Related Questions