python - Connecting Django web app database to postgresql on Pythonanywhere -
i want connect django web app database postgresql database have on pythonanywhere paid account. before coding anything, wanted talking each other. settings.py database section django app. i'm running python 3.5 , 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 , port we're both provided pythonanywhere.com site under tab database , postgres. did create database, username, , password on postgres console.
i created checkedb.py script found check if connection 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 error receive after running 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) none keyerror: 'django_settings_module' during handling of above exception, 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, settings not configured. must either define environment variable d jango_settings_module or call settings.configure() before accessing settings.
the directory project looks this
golf/ ---golf/ ------__init.py__ ------dbcheck.py ------settings.py ------urls.py ------wsgi.py ---media/ ---static/ ---manage.py
you need setup django first if using standalone script. have been easier try ./manage.py shell
. if want test 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() django.db import connections db_conn = connections['default'] try: c = db_conn.cursor() except operationalerror: connected = false else: connected = true
Comments
Post a Comment