Django settings.py + dj_database_url在Heroku上? [英] Django settings.py + dj_database_url on Heroku?

查看:128
本文介绍了Django settings.py + dj_database_url在Heroku上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

我是应该像上面那样离开这个?这就是我所做的 - 我的网站似乎工作得很好......





我应该在DATABASE_URL中换出一些东西 - 从heroku config设置中取出?



由于没有在任何地方使用DATABASE_URL,它似乎在heroku上工作上面的设置和没有改变....但当我看着数据库与pg:信息 - 我得到0连接...这让我觉得有没有连接到postgresql - 但表明它的工作呢? ?



我还不完全了解dj_database_url如何在Heroku上工作......任何人都可以在这里看到更多的细节?

  heroku pg:info 
=== HEROKU_POSTGRESQL_GOLD_URL(DATABASE_URL)
计划:Dev
状态:可用
连接:0
PG版本:9.1.6
创建日期:2012-10-07 16:11 UTC
数据大小:6.6 MB
表格数量:12
行数:27/10000(合规)
分叉/关注:不可用


解决方案

这是正确的。



在Heroku上运行时,有一个环境变量集( DATABASE_URL ),它包含数据库URL(像 postgres:// ,但是有一个自动生成的用户名/密码/数据库名,并且主机通常在amazonaws上)



在本地运行时,未设置 DATABASE_URL ,因此将使用 default ='...'数据库URL允许您在本地运行代码进行开发,并部署到Heroku,而不需要更改任何代码)。
$ b 这是基于12factor方法论(整个文档非常详细地描述了Heroku的结构)



code> dj_database_url.config 只是解析URL中的用户名/密码/主机/数据库名称,并将其分成 Django预期的字典格式 - 代码很简单,如果你好奇的话

Im following the getting started with Django on Heroku - and it shows to set up dj_database_url...

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

Am I supposed to leave this exactly as above? thats what I did - and my site appears to work just fine...

OR

Am I supposed to swap out something in the above with DATABASE_URL - which gets pulled from the heroku config set ups?

With not using DATABASE_URL anywhere -- it appears to be working on heroku with the above set up and no change.... but when I look at the DB with a pg:info - I get 0 connections... which makes me think there is no connection to the postgresql -- but hows it working then??

I don't quite understand yet how dj_database_url is working on Heroku with this...can anyone shed a bit more light on this?

heroku pg:info
=== HEROKU_POSTGRESQL_GOLD_URL (DATABASE_URL)
Plan:        Dev
Status:      available
Connections: 0
PG Version:  9.1.6
Created:     2012-10-07 16:11 UTC
Data Size:   6.6 MB
Tables:      12
Rows:        27/10000 (In compliance)
Fork/Follow: Unavailable

解决方案

It is correct as-is.

When running on Heroku, there is an environment variable set (DATABASE_URL) which contains the database URL (a string like postgres://, but with a long autogenerated username/password/database-name, and the host is on amazonaws usually)

When running locally, DATABASE_URL is not set, so your default = '...' database URL is used instead (allows you to run the code locally for development, and deploy to Heroku, without changing any code).

This is based on the "12factor methodology" (the whole document pretty much describes how Heroku is structured)

The dj_database_url.config just parses the username/password/host/db-name from the URL, and splits it into the dictionary format expected by Django - the code is rather simple, if you are curious

这篇关于Django settings.py + dj_database_url在Heroku上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆