0

So Ive managed to screw something up between my production database settings for heroku.

Running the production settings locally I receive the error,

ImproperlyConfigured at /
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

And I get the following error when deployed to Heroku seen here: http://tulsa-staging.herokuapp.com

Exception Value:    

cannot concatenate 'str' and 'NoneType' objects

It appears that it has to do with my Database settings but I'm just not sure how to resolve this.

prod.py database settings for Heroku

urlparse.uses_netloc.append('postgres')
urlparse.uses_netloc.append('mysql')

try:
    if 'DATABASE_URL' in os.environ:
        url = urlparse.urlparse(os.environ['DATABASE_URL'])

        DATABASES = {
            'default':{
                'ENGINE':'django.db.backends.postgresql_psycopg2',
                'NAME': url.path[1:],
                'USER': url.username,
                'PASSWORD': url.password,
                'HOST': url.hostname,
                'PORT': url.port
            }
        }

except Exception:
    print 'Unexpected error:', sys.exc_info()

Any thoughts on how to resolve this?

harristrader
  • 1,181
  • 2
  • 13
  • 20

1 Answers1

1

Why aren't you using dj-database-url?

It makes this so much easier:

import dj_database_url

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

It automatically looks for and parses the value in env['DATABASE_URL'], falling back to what you pass into default.

It's what Heroku actually recommends you use.

Jack Shedd
  • 3,501
  • 20
  • 27
  • Can you provide a complete example of `dj_database_url` configuration in settings.py? I couldn't find any of them in google search. – arulmr Feb 27 '13 at 08:14
  • My answer is literally exactly that. It's all you need. After you install dj_database_url into your virtualenv, just paste that line into your settings.py, replacing your existing DATABASES set. – Jack Shedd Feb 27 '13 at 08:16
  • Thanks Jack. I use this line `DATABASES = {'default': dj_database_url.config(default='mysql://localhost')}` and it works absolutely fine. But where do I specify my mysql_database name, username and password? And what if I want to use different database settings for production and development version? This is not related to this question. But I got these doubts while trying the solution in my project. It will be great, if you could help me. – arulmr Feb 27 '13 at 08:23
  • 1
    It's a basic netloc, so you just specify it like mysql://user:pass@localhost/database – Jack Shedd Feb 27 '13 at 08:32
  • For production, assuming you're on heroku, you don't need to do anything. dj_database_url will pick up the DATABASE_URL from heroku's env. For local stuff, I usually set that 'default' value equal to my local connection credentials. – Jack Shedd Feb 27 '13 at 08:34
  • Superb. It works like charm in my development server. For production I'm using [webfaction](http://www.webfaction.com). Will it be the same format there also? – arulmr Feb 27 '13 at 08:43
  • 1
    I'm not super familiar with webfaction. There's no reason it shouldn't "work", but obviously the credentials may need to be different. I don't know if webfaction uses environment variables the same way heroku does. – Jack Shedd Feb 27 '13 at 08:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25207/discussion-between-arulmr-and-jack-shedd) – arulmr Feb 27 '13 at 08:48
  • Thanks you for your response . Using the settings you recommended I still receive the same error once I attempt to login. – harristrader Feb 27 '13 at 12:21
  • Resolved my issue here. Didnt have any thing to do with the database settings. Discovered my secret_key was improperly set in the heroku environment. – harristrader Feb 28 '13 at 12:27