Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Database details from settings.py

I am a newbie in python. I need to access a python file from another file in another directory. In particular I want to be able to see these database details which are in a file named settings.py:

DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dynamic',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '192.168.10.130',
    }
}

How do I access this dictionary as a database from another file in another folder?

I have tried to do it like this (which works in my other project):

from django.conf import settings
dbHost = settings.DATABASES['default']['HOST']
dbUsername = settings.DATABASES['default']['USER']
dbPassword = settings.DATABASES['default']['PASSWORD']
dbName = settings.DATABASES['default']['NAME']

Please help me. Thanks in advance.

like image 204
bob marti Avatar asked Feb 28 '17 14:02

bob marti


People also ask

How can I see my Django database?

If you're interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MariaDB, MySQL), .tables (SQLite), or SELECT TABLE_NAME FROM USER_TABLES; (Oracle) to display the tables Django created.

What is Allowed_hosts in Django settings?

ALLOWED_HOSTS. A list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.

What does the settings py file do?

settings.py is a core file in Django projects. It holds all the configuration values that your web app needs to work; database settings, logging configuration, where to find static files, API keys if you work with external APIs, and a bunch of other stuff.

What is Csrf_cookie_secure?

OpenStack Horizon - CSRF_COOKIE_SECURE parameter set to True CSRF (Cross-site request forgery) is an attack which forces an end user to execute unauthorized commands on a web application in which he/she is currently authenticated.


2 Answers

If you want to use projectA.settings.DATABASES in projectB then you should add projectA path in projectB:

projectB/myfile.py:

import sys
sys.path.append( "/path/to/projectA" )
from projectA import settings

dbHost     = settings.DATABASES['default']['HOST']
dbUsername = settings.DATABASES['default']['USER']
dbPassword = settings.DATABASES['default']['PASSWORD']
dbName     = settings.DATABASES['default']['NAME']
like image 63
Sdra Avatar answered Oct 17 '22 07:10

Sdra


from django.conf import settings
settings.DATABASES
{'default': 
    {'ATOMIC_REQUESTS': False,
    'AUTOCOMMIT': True,
    'CONN_MAX_AGE': 3600,
    'ENGINE': 'django.db.backends.mysql',
    'HOST': '127.0.0.1',
    'NAME': 'proj',
    'OPTIONS': {'charset': 'utf8', 'sql_mode': 'traditional'},
    'PASSWORD': 'password',
    'PORT': '20306',
    'TEST': {'CHARSET': None, 'COLLATION': None, 'MIRROR': None, 'NAME': None},
    'TIME_ZONE': None,
    'USER': 'root'}
}
like image 43
ramwin Avatar answered Oct 17 '22 06:10

ramwin