简体   繁体   English

为什么在设置中使用os.environ导致Django无法运行?

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

I'm running Django 1.5.1. 我正在运行Django 1.5.1。 In my settings.py file, I have: 在我的settings.py文件中,我有:

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' . 当我尝试运行python manage.py runserver --settings=foo.local_settings ,它抛出Unknown command: 'runserver' If I take out the os.environ parts, though, it works. 但是,如果我取出os.environ部分,它就可以工作。

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. 请参阅我对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. 基本上,我是from settings import *进行local_settings.py的from settings import * ,而这又引发了一个错误,即尚未设置os.environ['DATABASE_URL'] I moved the import line under the rest of the stuff in local_settings.py, and now it works. 我将导入行移到了local_settings.py中其余内容的下面,现在可以使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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