简体   繁体   中英

Why does using os.environ in settings cause Django not to run?

I'm running Django 1.5.1. In my settings.py file, I have:

import os, dj_database_url

# ...

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

And I have a local settings file with:

os.environ['DATABASE_URL'] = 'mysql://root@localhost/my-database'

When I try to run python manage.py runserver --settings=foo.local_settings , it throws Unknown command: 'runserver' . If I take out the os.environ parts, though, it works.

I can't figure what's going on; does anyone have any ideas? Also, if there is a better way to approach what I'm doing - that is, offload the environment settings to local values - please let me know. Thanks!

See my comment to Guandalino. Basically I had local_settings.py doing from settings import * , which in turn was throwing an error that os.environ['DATABASE_URL'] was not yet set. I moved the import line under the rest of the stuff in local_settings.py, and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM