Reputation: 21
I want to connect my Django web app database to my postgresql database I have on my Pythonanywhere paid account. Before coding anything, I just wanted to get everything talking to each other. This is the settings.py DATABASE section from my django app. I'm running Python 3.5 and Django 1.9.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '[myDatabaseName]',
'USER': '[myUsername]',
'PASSWORD': '[myPassword]',
'HOST': 'xxxxxxxx-xxx.postgres.pythonanywhere-services.com',
'PORT': '10130',
}
}
The HOST and PORT we're both provided from the pythonanywhere.com site under the tab DATABASE and Postgres. I did create my database, username, and password on the postgres console.
I then created a checkedb.py script I found that would check if the connection with the postgres database works.
from django.db import connections
db_conn = connections['default']
try:
c = db_conn.cursor()
except OperationalError:
connected = False
else:
connected = True
This is the error I receive after running this code.
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 38, in _setup
settings_module = os.environ[ENVIRONMENT_VARIABLE]
File "/usr/lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/giraldez/golf/golf/dbcheck.py", line 2, in <module>
db_conn = connections['default']
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 196, in __getitem__
self.ensure_defaults(alias)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 170, in ensure_defaults
conn = self.databases[alias]
File "/usr/local/lib/python3.4/dist-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 153, in databases
self._databases = settings.DATABASES
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable D
JANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
The directory for my project looks like this
golf/
---golf/
------__init.py__
------dbcheck.py
------settings.py
------urls.py
------wsgi.py
---media/
---static/
---manage.py
Upvotes: 2
Views: 1633
Reputation: 174624
The error you are getting is because you need to properly initialize the django environment before you can write custom scripts against it.
The easiest way to solve this is to run a python shell that already has the django configuration loaded, you can do this with python manage.py shell
.
Once this shell has loaded, enter your code and it should tell you if the connection is valid or not.
Upvotes: 1
Reputation: 53734
You need to setup django first if you are using it as a standalone script. Would have been easier to try with ./manage.py shell
. but if you want to test with a standalone script, here goes:
import sys,os
if __name__ == '__main__': # pragma nocover
# Setup environ
sys.path.append(os.getcwd())
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings_dev")
import django
django.setup()
from django.db import connections
db_conn = connections['default']
try:
c = db_conn.cursor()
except OperationalError:
connected = False
else:
connected = True
Upvotes: 2