69026

Using Constants in Settings.py

Question:

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.

Answer1:

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 <a href="http://docs.djangoproject.com/en/dev/topics/settings/#using-settings-in-python-code" rel="nofollow">Using settings in Python code</a> explains why this works, and why this is preferable over importing the the settings.py module directly.

Answer2:

yes

from django.conf import settings print settings.MY_SETTINGS_VAR

Recommend

  • Symfony 2 - Attempted to call function “apcu_fetch” from namespace “Doctrine\\Common\\Cache”
  • Fatal error: Cannot use try without catch or finally in /users/xxx/www/index.php on line 20
  • Oracle database error in symfony2 (doctrine). Is parameters.yml setup correctly?
  • Connection to a db with Doctrine pdo_sqlsrv
  • Symfony 3 - EntityManager dependency injection with multiple db connections
  • How to bring front or launch browser in Delphi
  • Case for having xmlhttprequest available in sync
  • Import vis.js with webpack
  • Replacing NaT with Epoch in Pandas
  • Unable to restore SQL database, exclusive access could not be obtained (single user mode)
  • SQLException: the result set is closed
  • Edit sqlite file from terminal Mac
  • How to break up an array into strings MVC?
  • Sqlite: Execute Spatialite Functions in Intellij
  • Xampp PHP server GET works Fine POST isn't working
  • How to run .ear file in JBoss 6?
  • Azure Resource Template Deployment issues
  • Deploy same Javascript webapp build to different environments
  • Trouble connecting to Google Cloud SQL server from deployed app
  • Microsoft Chart Controls for Microsoft .NET Framework 4.0
  • Trying to get the char code of ENTER key
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Refering to the class itself from within a class mehod in Objective C
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Is there any way to access browser form field suggestions from JavaScript?
  • What is the “return” in scheme?
  • C# - Serializing and deserializing static member
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Sending data from AppleScript to FileMaker records
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • How to include full .NET prerequisite for Wix Burn installer
  • How do you join a server to an Active Directory (domain)?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • coudnt use logback because of log4j
  • Is it possible to post an object from jquery to bottle.py?
  • How to get NHibernate ISession to cache entity not retrieved by primary key