Using constants in Settings.py

Can I use a variable declared in a Django project settings.pyin one of my module files?

For example, using DATABASE_HOST = 'databasename'?

I am trying to get the name of the server where the application is currently deployed.

+3
source share
2 answers

Of course you can ... it encouraged, in fact. To use it, import the settings from django.conf (this will import your project settings):

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

The documentation for Using Settings in Python Code explains why this works, and why it is preferable to importing the module directly settings.py.

+13
source

Yes

from django.conf import settings

print settings.MY_SETTINGS_VAR
+4
source

Source: https://habr.com/ru/post/1724961/


All Articles