2👍
Did you try the stuff in the documentation at https://devcenter.heroku.com/articles/django#django-settings?
import dj_database_url
DATABASES['default'] = dj_database_url.config()
It says
Django settings
Next, configure the application for the Heroku environment, including
Heroku’s Postgres database. The dj-database-url module will parse the
values of the DATABASE_URL environment variable and convert them to
something Django can understand.Make sure ‘dj-database-url’ is in your requirements file, then add the
following to the bottom of your settings.py file:
settings.py
# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
6👍
Both Sultan and Joe are correct, and if you put them together and dig a little it works, I just want to lay it out a little clearer with an addition that helped me. These instructions on the Heroku site explain how to set it up including what Sultan mentioned, adding this section to settings.py:
# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Then, (and what those instructions don’t mention) in your Heroku account go to your Databases section and select the database you would like to use. Go to ‘Connection Settings’ (two little arrows going opposite directions) and select the Django option. That code should look like what Joe mentioned:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'your_db_name',
'HOST': 'your_host',
'PORT': '5432',
'USER': 'your_db_user_name',
'PASSWORD': 'your_password',
}
}
Paste this at the bottom of your settings.py file and sync your DB.
- [Django]-Django – Using context_processor
- [Django]-Django – Dynamically importing a models form
- [Django]-Understanding / mySQL aka tricking ForeignKey relationships in Django
- [Django]-Can't get media files in heroku
- [Django]-Remove keys from array in django rest framework serializer
2👍
Your are defining the DATABASES = {'default': {... }}
twice.
Solution:
If you are using heroku to serve your database then use this
import dj_database_url
DATABASES['default'] = dj_database_url.config()
if you’re trying to connect to an external database server comment the two lines above and use this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
- [Django]-Avoid Circular import in Django
- [Django]-Are booleans mutable in python?
- [Django]-Django(Python) AttributeError: 'NoneType' object has no attribute 'split'
- [Django]-Django static files not loading
- [Django]-Django multi-tenant
1👍
Why don’t you try this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'yourdatabasename',
'HOST': 'localhost',
'PORT': '',
'USER': 'yourusername',
'PASSWORD': 'yourpassword',
}
}
and after that apply this:
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
- [Django]-Caching results of a Django function call with cache.get_or_set()
- [Django]-Django i18n_patterns without trailing slash
0👍
You haven’t defined any of the database info.
Your DATABASES section should look something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'your_db_name',
'USER': 'your_db_user_name',
'PASSWORD': 'your_password',
'HOST': 'ec2-23-21-133-106.compute-1.amazonaws.com', # Or something like this
'PORT': '5432',
}
}