【Django×Google App Engine】No module named 'MySQLdb' when deploying - django

I'm setting GitHub source ↓
https://github.com/priyankavergadia/Django-Dialogflow-GoogleVisionAPI
But I have trouble in [Deploy the app to the App Engine standard environment] phase.
At a glance Deploying have been done well.But I'm trying to access webservice, so [502 Bad Gateway] appears.
At local environment(running python manage.py runserver), this program works well.
Please give me some advices.
My environment:
Windows10
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
django 2.2.4
MySQL second generation 5.7
mysql django
I serched and tried to:
install mysqlclient and uninstall PyMySQL
rewrite [import ~]part in various pattern...
description in Django setting.py below
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
# Install PyMySQL as mysqlclient/MySQLdb to use Django's mysqlclient adapter
# See https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-db-api-drivers
# for more information
import MySQLdb as sql# noqa: 402
# [START db_setup]
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/michatbot-250809:us-central1:polls-instance2',
'USER': 'test',
'PASSWORD': '',
'NAME': 'polls',
}
}
else:
# Running locally so connect to either a local MySQL instance or connect to
# Cloud SQL via the proxy. To start the proxy via command line:
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
#
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'polls',
'USER': 'test',
'PASSWORD': '',
}
}
# [END db_setup]
eroor log on Google App Engine below
textPayload: "Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 104, in init_process
super(ThreadWorker, self).init_process()
File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
self.load_wsgi()
File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
self.wsgi = self.app.wsgi()
File "/env/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
return self.load_wsgiapp()
File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
return util.import_app(self.app_uri)
File "/env/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
__import__(module)
File "/srv/main.py", line 1, in <module>
from mysite.wsgi import application
File "/srv/mysite/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/env/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/env/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/env/lib/python3.7/site-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/env/lib/python3.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/env/lib/python3.7/site-packages/django/conf/__init__.py", line 107, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/opt/python3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/srv/mysite/settings.py", line 82, in <module>
import MySQLdb as sql # noqa: 402
ModuleNotFoundError: No module named 'MySQLdb'"

There are two threads where the community figured out several ways to solve this error depending on your SO and software's versions. You can refer to these threads, second one, and also to this one that addresses discussion more about connecting MySQL in Python 3 on Windows.
Make sure to follow the documentation about Python 3.7 in the GAE Standard. Also, GAE has some specific support for using Django, I'd suggest going through the Django Support documentation and Django App example.

Related

.env keyError in django project

I'm having a hard time because my django project.
Who can help?
I'd like to thank you for your help.
set .env in proeject Root path
SECRET_KEY=exampleKey
DEBUG=False
DB_ENGINE=django.db.backends.mysql
DB_NAME=example
DB_USER=admin
...Blah Blah
project settings.py
import os
import environ
env = environ.Env(DEBUG=(bool, False), )
environ.Env.read_env()
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
...Blah Blah
And when run the project
I get an this error message.
Django_workspace/asone/asone/.env doesn't exist - if you're not configuring your environment separately, create one.
"environment separately, create one." % env_file)
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
During handling of the above exception, another exception occurred:
/Django_workspace/asone/venv/lib/python3.7/site-packages/environ/environ.py", line 277, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
I have a hard time because I'm a django beginner.
Who can help? I'd like to thank you for your help.
.P.S
Thank you so much for your answer.
I'm sorry. I'm telling you, there's something I missed out on.
I set up a django-environ package to use the .env in the project.
pip install django-environ
Is there something I'm missing?
What more information will be needed to solve this problem?
.P.S
set up python-dotenv package
pip install python-dotenv.
Change settings.py
import os
from dotenv import load_dotenv
load_dotenv()
...
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG=os.getenv('DEBUG')
DATABASES = {
'default': {
# 'ENGINE': env('DB_ENGINE'),
# 'NAME': env('DB_NAME'),
# 'USER': env('DB_USER'),
# 'PASSWORD': env('DB_PASSWORD'),
# 'HOST': env('DB_HOST'),
# 'PORT': env('DB_PORT'),
'ENGINE': os.getenv('DB_ENGINE'),
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': os.getenv('DB_PORT'),
}
}
...
and then I met this error message.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 562, in configure
handler = self.configure_handler(handlers[name])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 735, in configure_handler
result = factory(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/handlers.py", line 148, in __init__
BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/handlers.py", line 55, in __init__
logging.FileHandler.__init__(self, filename, mode, encoding, delay)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1087, in __init__
StreamHandler.__init__(self, self._open())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1116, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/logs/logfile'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/log.py", line 75, in configure_logging
logging_config_func(logging_settings)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 799, in dictConfig
dictConfigClass(config).configure()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 570, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'file'
Is there something I'm missing?
There is no environ module in stdlib(standard library of python). So I checked if there is third party module called 'environ' by running pip install environ, which installed it, but when I checked at pypi website, I can't see any valid explanation. I think it's not maintained well.
So I concluded that your environ configuration went a bit odd.
Here's my suggestion
Install python-dotenv module by running pip install python-dotenv. There are many .env module, but this is one of the most popular, well maintained one in my opinion.
Ensure your .env file is in the directory where settings.py is. setting.py will load .env file in the same directory unless specified otherwise
Change your code from this
import os
import environ
env = environ.Env(DEBUG=(bool, False), )
environ.Env.read_env()
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
to this
import os
from dotenv import load_dotenv
load_dotenv()
...
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG=os.getenv('DEBUG')
...
Change your db setting similar way
DATABASES = {
'default': {
'ENGINE': os.getenv('DB_ENGINE'),
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
# and so on ...
}
}
}
You are running into a KeyError because either:
You aren't putting a SECRET_KEY variable in your .env file.
It can't find your .env file.
From what I can see from your answer, try moving your .env file from your root directory to where it will sit alongside settings.py.

django-pyodbc-azure no module named 'sql_server'

I am attempting to use a corporate SQL Server as the backend for my Django project with Python 3.5.2. I have installed Django 2.0.3, pyodbc==4.0.22, django-pyodbc-azure 2.0.3, pypiwin32==219 (pip freeze below). I have also configured DATABASES in my site's settings.py (see below). When attempting to run the server I get the following error: No module named 'sql_server'. I referenced other questions, the most relevant of which appeared to be: No module named sql_server.pyodbc.base, which concluded that the django-pyodbc-azure and Django versions needed to be identical. This did not solve my problem, however; the same error persists.
Traceback (most recent call last):
File "manage.py", line 24, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\K5SH\venv\lib\site-packages\django\core\management\__init__.py"
, line 338, in execute_from_command_line
utility.execute()
File "C:\Users\K5SH\venv\lib\site-packages\django\core\management\__init__.py"
, line 312, in execute
django.setup()
File "C:\Users\K5SH\venv\lib\site-packages\django\__init__.py", line 18, in se
tup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\K5SH\venv\lib\site-packages\django\apps\registry.py", line 108,
in populate
app_config.import_models(all_models)
File "C:\Users\K5SH\venv\lib\site-packages\django\apps\config.py", line 198, i
n import_models
self.models_module = import_module(models_module_name)
File "C:\Users\K5SH\AppData\Local\Programs\Python\Python35-32\lib\importlib\__
init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\K5SH\venv\lib\site-packages\django\contrib\auth\models.py", lin
e 41, in <module>
class Permission(models.Model):
File "C:\Users\K5SH\venv\lib\site-packages\django\db\models\base.py", line 139
, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "C:\Users\K5SH\venv\lib\site-packages\django\db\models\base.py", line 324
, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\K5SH\venv\lib\site-packages\django\db\models\options.py", line
250, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length(
))
File "C:\Users\K5SH\venv\lib\site-packages\django\db\__init__.py", line 36, in
__getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\K5SH\venv\lib\site-packages\django\db\utils.py", line 240, in _
_getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\K5SH\venv\lib\site-packages\django\db\utils.py", line 129, in l
oad_backend
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an availa
ble database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'base', 'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3'
Error was: No module named 'sql_server'
Installed packages:
chardet==2.3.0
configparser==3.5.0
cycler==0.10.0
diff-match-patch==20121119
Django==2.0.3
django-crispy-forms==1.7.2
django-csvimport==2.4
django-debug-toolbar==1.9.1
django-import-export==1.0.0
django-money==0.9.1
django-mssql==1.8
django-pyodbc-azure==2.0.3.0
djangorestframework==3.6.3
et-xmlfile==1.0.1
jdcal==1.3
matplotlib==1.5.3
mysqlclient==1.3.12
numpy==1.11.2rc1+mkl
openpyxl==2.4.8
pandas==0.18.1
psycopg2==2.7.3.2
pymssql==2.1.2
pyodbc==4.0.22
pyparsing==2.1.9
pypiwin32==219
pypyodbc==1.3.3
python-dateutil==2.5.3
pytz==2016.6.1
scipy==0.18.1
seaborn==0.7.1
six==1.10.0
sqlalchemy==1.1.4
sqlparse==0.2.4
tablib==0.10.0
virtualenv==15.0.3
xlrd==1.0.0
xlwt==1.2.0
Databases:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'my_db_name',
'USER': '',
'PASSWORD': '',
'HOST': 'my_sql_host',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
(Note: I have attempted this both with and without the 'OPTIONS' setting).
I started fresh on Windows using Python 3.6.0 and virtualenv 15.1.0. These should be close enough that this should work.
First I created and activated a virtualenv, and installed django-pyodbc-azure, which in turn installed Django and pyodbc as dependencies:
virtalenv djangowin
djangowin\Scripts\activate.bat
pip install django-pyodbc-azure
Then I started a fresh Django project:
django-admin startproject sqltest
Then I edited sqltest\sqltest\settings.py and updated my DATABASES definition to use a freshly created database:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'cdw-sql0101.wharton.upenn.edu',
'PORT': '1433',
'NAME': 'flipperpa',
'USER': 'flipperpa',
'PASSWORD': '',
'AUTOCOMMIT': True,
'OPTIONS': {
'driver': 'SQL Server',
'unicode_results': True,
'host_is_server': True,
},
}
}
...and it worked like a charm:
(djangowin) C:\Users\tallen\sqltest>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
March 23, 2018 - 16:39:51
Django version 2.0.3, using settings 'sqltest.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Is there some step along this path that didn't work for you? Are you sure your virtualenv is active? The command djangowin\Scripts\activate.bat is very important - you're going to do that each time you open a new terminal window.
Default virtual environment doesn't work properly on azure. Activate command seems has no effect. But if you create your own virtual environment on azure it works properly.
I faced the same kind of problem for my project then I installed virtual environment for myself and edited the auto deployment configurations scripts. Now it is working perfectly.
I found this issue mainly due to Version of django I replaced my Django version with django==2.1.15 then it worked.
django-pyodbc-azure==2.1.0.0
pyodbc=4.0

Connect Django with SAP Hana

I'm trying to connect django with a SAP Hana DB. I've installed a backend as described in github: https://github.com/kapilratnani/django_hana
and
configured the settings.py as specified:
'data_source': {
'ENGINE':'django.db.backends.django_hana',# I tried also 'django_hana' or 'hello' with the same results
'NAME': 'IOT',
'USER': 'ALEX',
'PASSWORD': 'PASSWORD',
'HOST': '186.47.255.17',
'PORT': '30015',
}
I'm getting a connection error, as if it is not recognizing the Engine: I get the same error if I type 'hello' in the engine. Here is the error:
Unhandled exception in thread started by <function wrapper at 0x7fc2a959f0c8>
Traceback (most recent call last):
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/core/checks/model_checks.py", line 28, in check_all_models
errors.extend(model.check(**kwargs))
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/db/models/base.py", line 1172, in check
errors.extend(cls._check_long_column_names())
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/db/models/base.py", line 1587, in _check_long_column_names
connection = connections[db]
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/django/Env/singolar/lib/python2.7/site-packages/django/db/utils.py", line 135, in load_backend
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.django_hana' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: No module named django_hana.base
Any ideas why it's not recognizing the Engine?
Found repository that supports Django 1.9 and resolves the error: github.com/mathebox/django_hana_pyhdb

How do I get south to work on jython2.7-django1.7?

I hoping that you can help me out. I currently have Django1.7 running on windows7/Java7/Jython2.7/Postgresql9.3/postgresql-9.3-1102.jdbc41.
For more details about django on jython and the database settings.
postgresql on jython-django
My settings are:
DATABASES = {
'default': {
'ENGINE': 'doj.db.backends.postgresql',
'NAME': 'lwc',
'USER': 'lwc',
'PASSWORD': 'lwc',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
My Question:
I am not able to install South by using pip. So, I just installed it manually from the source. Afterwards I do jython manage.py syncdb
I then get an error... Do you have any ideas how to resolve this error?
C:\Users\michmar3\workspace\lwc>jython manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\jython2.7b2\Lib\site-packages\django-1.7c3-py2.7.egg\django\core\mana
gement\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "C:\jython2.7b2\Lib\site-packages\django-1.7c3-py2.7.egg\django\core\mana
gement\__init__.py", line 354, in execute
django.setup()
File "C:\jython2.7b2\Lib\site-packages\django-1.7c3-py2.7.egg\django\__init__.
py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\jython2.7b2\Lib\site-packages\django-1.7c3-py2.7.egg\django\apps\regi
stry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\jython2.7b2\Lib\site-packages\django-1.7c3-py2.7.egg\django\apps\conf
ig.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "C:\jython2.7b2\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\jython2.7b2\Lib\site-packages\south-1.0-py2.7.egg\south\models.py", l
ine 2, in <module>
from south.db import DEFAULT_DB_ALIAS
File "C:\jython2.7b2\Lib\site-packages\south-1.0-py2.7.egg\south\db\__init__.p
y", line 84, in <module>
db = dbs[DEFAULT_DB_ALIAS]
KeyError: 'default'
south will not work with django 1.7. http://south.aeracode.org/
The functionality that south used to provide has been directly merged into django with the 1.7 release.
See the django documentation for how to use the django migration support that replaces south. https://docs.djangoproject.com/en/1.7/topics/migrations/

Django 1.3.1 Heroku Postgres error

We recently pushed our site to staging and have been struggling to get it up ever since, and the team at Heroku are not really responding in time, so I am turning to the community to see if there is a quick fix.
We scrapped the old one and set up a new stack still with the same issues
heroku config
DISABLE_INJECTION: 1
settings.py
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'PORT': '',
'HOST': 'localhost'
},
}
Here is the full trace.
heroku run python myapp/manage.py syncdb
Traceback (most recent call last):
File "fundedbyme/manage.py", line 11, in <module>
execute_manager(settings)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/app/.heroku/venv/lib/python2.7/site-packages/south/management/commands/syncdb.py", line 90, in handle_noargs
syncdb.Command().execute(**options)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 56, in handle_noargs
cursor = connection.cursor()
File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/__init__.py", line 252, in cursor
cursor = util.CursorWrapper(self._cursor(), self)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 140, in _cursor
self.connection = Database.connect(**conn_params)
File "/app/.heroku/venv/lib/python2.7/site-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
DATABASES = {
'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'))
}
Make sure you have Postgres on your heroku:
heroku addons:add heroku-postgresql:dev
Figure out database url env variable. It's going to look something like this : HEROKU_POSTGRESQL__URL
heroku config | grep POSTGRESQL
Update your settings
import dj_database_url
import os
POSTGRES_URL = "HEROKU_POSTGRESQL_<COLOR>_URL"
DATABASES = {'default': dj_database_url.config(default=os.environ[POSTGRES_URL])}
Bob is your uncle.
I put together a handy bootstrap for django on heroku. It might be helpful: https://github.com/callmephilip/django-heroku-bootstrap
Happy deploying
Try this:
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://myuser:mypassword#localhost:5432/mydb')}