Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku Database Settings Injection - How do I setup my dev django database?

Tags:

django

heroku

I'm trying to get my local dev django app to work after following these instructions on adding env database settings.

https://devcenter.heroku.com/articles/django-injection

I followed the instructions but get the following error when my app tries to access the local database

Request Method: GET Request URL:    http://localhost:8000 Django Version: 1.4 Exception Type: ImproperlyConfigured Exception Value:     You need to specify NAME in your Django settings file. 

My database settings originally,

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.postgresql_psycopg2',         'NAME': 'db',                      # Or path to database file if using sqlite3.         'USER': 'foo',                      # Not used with sqlite3.         'PASSWORD': 'bar',                  # Not used with sqlite3.         'HOST': 'localhost',                         'PORT': '5432',     } } 

the heroku article says to add the following to the settings file

import dj_database_url DATABASES = {'default': dj_database_url.config(default='postgres://localhost')} 

how do I get dj_database_url.config to use my my dev settings when the DATABASE_URL is not available in dev?

like image 287
heifetz Avatar asked Jun 17 '12 13:06

heifetz


2 Answers

You can just add your dev settings to the default values like this...

import dj_database_url DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar@localhost:5432/db')} 
like image 183
kyen99 Avatar answered Sep 20 '22 17:09

kyen99


Use this in your settings.py:

DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])} 

and in your .env file have this:

DATABASE_URL=postgres://localhost/yourdbname 

when you launch with "foreman start" it will look at the .env file and create all those environment variables, just like running on Heroku itself. Type "heroku config" to confirm that you have a DATABASE_URL set, which you should if you added the postgres database addon.

like image 23
user712225 Avatar answered Sep 17 '22 17:09

user712225