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.
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.
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.
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.
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.
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']
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'}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With