2

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.

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

1 Comment

for some constants, it would be interesting to use getattr() getattr(settings, "ITEMS_PER_PAGE", 25)
3

yes

from django.conf import settings

print settings.MY_SETTINGS_VAR

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.