I'm trying to get my local dev django app to work after following these instructions on adding env database settings.
在遵循了关于添加env数据库设置的说明之后,我正在尝试让我的本地dev django应用程序正常工作。
https://devcenter.heroku.com/articles/django-injection
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
heroku的文章说要将以下内容添加到设置文件中
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?
如何获得dj_database_url。设置在开发中无法使用DATABASE_URL时使用我的开发设置?
5 个解决方案
#1
38
You can just add your dev settings to the default values like this...
您可以将您的dev设置添加到如下所示的默认值中……
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar@localhost:5432/db')}
#2
13
Use this in your settings.py:
在你的设置中使用这个。
DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])}
and in your .env file have this:
在你的。env文件中有:
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.
使用“foreman start”启动时,它将查看.env文件并创建所有这些环境变量,就像在Heroku上运行一样。输入“heroku config”以确认您有一个DATABASE_URL集,如果您添加了postgres数据库addon,那么应该这样做。
#3
8
Just set an environment variable on your operating system and check weither or not it's set. For instance, with a UNIX system:
只需在操作系统上设置一个环境变量,检查它是否设置好。
# In ~/.bash_profile
export LOCAL_DEV=true
# In settings.py
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
if bool(os.environ.get('LOCAL_DEV', False)):
# Override DATABASES['default'] with your local database configuration
Also, if you need to set an environment variable on your heroku space:
此外,如果您需要在heroku空间上设置一个环境变量:
heroku config:add MY_VAR='my_value'
#4
5
I just tried this and here is my code:
我刚试过这个,这是我的代码:
import dj_database_url
local_db = 'postgres://django_login:123456@localhost/django_db'
DATABASES = {'default': dj_database_url.config(default=local_db)}
My database name is "django_db", user name is "django_login", password is "123456".
我的数据库名称是“django_db”,用户名是“django_login”,密码是“123456”。
My code can run both in local machine and heroku.
我的代码可以在本地机器和heroku中运行。
#5
1
import dj_database_url
进口dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://yourusername:yourpassword@yourhosturl:5432/yourdbname')}
数据库= {“默认”:dj_database_url.config(默认=“postgres:/ / yourusername:yourpassword@yourhosturl:5432 / yourdbname’)}
** Replace bold string with your database settings if you are using local database then replace yourhosturl by localhost
**将粗体字符串替换为数据库设置,如果使用本地数据库,则使用localhost替换主机url
#1
38
You can just add your dev settings to the default values like this...
您可以将您的dev设置添加到如下所示的默认值中……
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar@localhost:5432/db')}
#2
13
Use this in your settings.py:
在你的设置中使用这个。
DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])}
and in your .env file have this:
在你的。env文件中有:
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.
使用“foreman start”启动时,它将查看.env文件并创建所有这些环境变量,就像在Heroku上运行一样。输入“heroku config”以确认您有一个DATABASE_URL集,如果您添加了postgres数据库addon,那么应该这样做。
#3
8
Just set an environment variable on your operating system and check weither or not it's set. For instance, with a UNIX system:
只需在操作系统上设置一个环境变量,检查它是否设置好。
# In ~/.bash_profile
export LOCAL_DEV=true
# In settings.py
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
if bool(os.environ.get('LOCAL_DEV', False)):
# Override DATABASES['default'] with your local database configuration
Also, if you need to set an environment variable on your heroku space:
此外,如果您需要在heroku空间上设置一个环境变量:
heroku config:add MY_VAR='my_value'
#4
5
I just tried this and here is my code:
我刚试过这个,这是我的代码:
import dj_database_url
local_db = 'postgres://django_login:123456@localhost/django_db'
DATABASES = {'default': dj_database_url.config(default=local_db)}
My database name is "django_db", user name is "django_login", password is "123456".
我的数据库名称是“django_db”,用户名是“django_login”,密码是“123456”。
My code can run both in local machine and heroku.
我的代码可以在本地机器和heroku中运行。
#5
1
import dj_database_url
进口dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://yourusername:yourpassword@yourhosturl:5432/yourdbname')}
数据库= {“默认”:dj_database_url.config(默认=“postgres:/ / yourusername:yourpassword@yourhosturl:5432 / yourdbname’)}
** Replace bold string with your database settings if you are using local database then replace yourhosturl by localhost
**将粗体字符串替换为数据库设置,如果使用本地数据库,则使用localhost替换主机url