- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标:
我打算遵循“十二要素应用程序”方法在Heroku上构建Django应用程序。
介绍:
我正在遵循“Heroku上的Django入门”快速入门指南。目前,我具有以下目录结构:
~/Projects/
hellodjango_rep/
.env (empty)
.git
.gitignore
Procfile
requirements.txt
hellodjango/
manage.py
hellodjango/
__init__.py
settings/
urls.py
wsgi.py
import dj_database_url
DATABASES['default'] = dj_database_url.config()
$ heroku open
成功访问了浏览器中的应用程序,但在本地使用了:
dj_database_url.config()
返回了一个空字典。
mac-pol:hellodjango_rep oubiga$ python
>>> import os
>>> os.environ
{
'PROJECT_HOME': '/Users/oubiga/Projects'...
'PATH': '/usr/local/heroku/bin:/usr/local/share/python:/usr/local/bin:/Applications/Postgres.app/Contents/MacOS/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin'...
'HOME': '/Users/oubiga'...
'WORKON_HOME': '/Users/oubiga/Envs'...
'VIRTUALENVWRAPPER_HOOK_DIR': '/Users/oubiga/Envs'...
'PWD': '/Users/oubiga/Projects/hellodjango_rep'
}
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.hellodjango.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
web: gunicorn hellodjango.hellodjango.wsgi
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ python hellodjango/manage.py shell
>>> import os
>>> os.environ
{
'PROJECT_HOME': '/Users/oubiga/Projects'...
'PATH': '/Users/oubiga/Envs/hellodjango_venv/bin:/usr/local/heroku/bin:/usr/local/share/python:/usr/local/bin:/Applications/Postgres.app/Contents/MacOS/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin',
'HOME': '/Users/oubiga'...
'WORKON_HOME': '/Users/oubiga/Envs'...
'VIRTUAL_ENV': '/Users/oubiga/Envs/hellodjango_venv'...
'VIRTUALENVWRAPPER_HOOK_DIR': '/Users/oubiga/Envs'...
'PWD': '/Users/oubiga/Projects/hellodjango_rep'...
'DJANGO_SETTINGS_MODULE': 'hellodjango.settings'
}
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ heroku run python hellodjango/manage.py shell
>>> import os
>>> os.environ
{
'DATABASE_URL': 'postgres://dbuser:dbpassword@ec2-184-73-162-34.compute-1.amazonaws.com:5432/dbname',
'HEROKU_POSTGRESQL_ORANGE_URL': 'postgres://dbuser:dbpassword@ec2-184-73-162-34.compute-1.amazonaws.com:5432/dbname',
'LIBRARY_PATH': '/app/.heroku/vendor/lib', 'PWD': '/app'...
'DJANGO_SETTINGS_MODULE': 'hellodjango.settings',
'PYTHONHOME': '/app/.heroku/python'...
'PYTHONPATH': '/app/'...
'DYNO': 'run.9068',
'LD_LIBRARY_PATH': '/app/.heroku/vendor/lib'...
'HOME': '/app', '_': '/app/.heroku/python/bin/python',
'PATH': '/app/.heroku/python/bin:/usr/local/bin:/usr/bin:/bin'...
}
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ heroku config
=== damp-dusk-5382 Config Vars
DATABASE_URL: postgres://dbuser:dbpassword@ec2-184-73-162-34.compute-1.amazonaws.com:5432/dbname
HEROKU_POSTGRESQL_ORANGE_URL: postgres://dbuser:dbpassword@ec2-184-73-162-34.compute-1.amazonaws.com:5432/dbname
The twelve-factor app stores config in environment variables... Env vars are easy to change between deploys without changing any code; unlike config files.
dj_database_url.config()
返回一个空对象:Heroku论坛中的
... dj_database_url.config() gets the Postgres credentials from the Heroku environment variables. But, on your local machine, those variables aren't there. You have to put them in your /venv/bin/activate shell script... put the variables in there. something like
DATABASE_URL = "xxx"
export DATABASE_URL
For each thing it needs. Then... "deactivate"... and ..."activate" again to restart it.
... dj-database-url is a great utility, as it allows you to use exactly the same settings.py code in your development and production environments, as recommended in the "12 factor app principles"... what dj_database_url.config() is doing is looking for the DATABASE_URL environment variable, and then parsing it into Django's preferred format... if you haven't manually created and promoted a postgres DB on Heroku, DATABASE_URL will not be present and the ImproperlyConfigured error will be raised. Setting the default for dj_database_url.config() as your local DB URL is one way to make sure that your application will work in a development environment. However, it is not necessarily the only way. Perhaps a better alternative is to manually set DATABASE_URL in your local .env file. Then, when running your app locally using Foreman, it will be loaded as an environment variable and dj_database_url will find it. So your .env would contain:
DATABASE_URL=postgres://user:pass@localhost/dbname
Meaning that in
settings.py
you would only need to have:
DATABASES['default']= dj_database_url.config()
...The advantage of using a local environment variable instead of a single, hard-coded default is that your code will run in any environment where the DATABASE_URL is set. If you change the name of your local DB, or want to run your code on a different dev machine, you only need to update your .env file instead of tinkering with settings.py.
... trying to get your application to behave in a way such that:
- When you're running the app on your laptop, it uses your local Postgres server.
- When you're running the app on your staging Heroku app, it uses the Postgres server addon.
- When you're running the app on your production Heroku app, it uses the Postgres server addon.
The best way to accomplish this is by using environment variables!… Environment variables are the most elegant (and scalable) way to handle application configuration between different environments… Instead of having many settings files, define a single file: settings.py, and have it make use of environment variables to pull service information and credentials... On Heroku, you can set environment variables manually by running:
$ heroku config:set SOME_VARIABLE=some_value
... there's always Kenneth Reitz's great autoenv tool. This lets you define a simple .env file in your project directory… And each time you enter your project directory, those environment variables will be automatically set so that you don't have to do anything special! Just run your project and everything will work as expected:
python
manage.py runserver
DATABASE_URL
:
DATABASE_URL=postgres://dbuser:dbpassword@ec2-184-73-162-34.compute-1.amazonaws.com:5432/dbname
$ foreman start
命令时:
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
17:25:39 web.1 | started with pid 319
17:25:39 web.1 | 2013-09-11 17:25:39 [319] [INFO] Starting gunicorn 18.0
17:25:39 web.1 | 2013-09-11 17:25:39 [319] [INFO] Listening at: http://0.0.0.0:5000 (319)
17:25:39 web.1 | 2013-09-11 17:25:39 [319] [INFO] Using worker: sync
17:25:39 web.1 | 2013-09-11 17:25:39 [322] [INFO] Booting worker with pid: 322
http://0.0.0.0:5000
中打开我的应用程序:
17:26:59 web.1 | 2013-09-11 10:26:59 [322] [ERROR] Error handling request
17:26:59 web.1 | Traceback (most recent call last):
17:26:59 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 131, in handle_request
17:26:59 web.1 | respiter = self.wsgi(environ, resp.start_response)
17:26:59 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/dj_static.py", line 59, in __call__
17:26:59 web.1 | return self.application(environ, start_response)
17:26:59 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
17:26:59 web.1 | self.load_middleware()
17:26:59 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
17:26:59 web.1 | raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
17:26:59 web.1 | ImproperlyConfigured: Error importing middleware django.contrib.auth.middleware: "dlopen(/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @loader_path/../lib/libssl.1.0.0.dylib
17:26:59 web.1 | Referenced from: /Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/psycopg2/_psycopg.so
17:26:59 web.1 | Reason: image not found"
dj_database_url.config()
返回:
{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'HOST': 'ec2-23-21-196-147.compute-1.amazonaws.com',
'USER': 'dbuser',
'PASSWORD': 'dbpassword',
'PORT': 5432
}
DATABASE_URL
。我将“ec2-184-73-162-34.compute-1.amazonaws.com:5432”替换为“localhost:5000”。
$ deactivate
,然后再次是
$ workon hellodjango_venv
。
DATABASE_URL=postgres://dbuser:dbpassword@localhost:5000/dbname
$ foreman start
命令时:
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
17:38:41 web.1 | started with pid 687
17:38:41 web.1 | 2013-09-11 17:38:41 [687] [INFO] Starting gunicorn 18.0
17:38:41 web.1 | 2013-09-11 17:38:41 [687] [INFO] Listening at: http://0.0.0.0:5000 (687)
17:38:41 web.1 | 2013-09-11 17:38:41 [687] [INFO] Using worker: sync
17:38:41 web.1 | 2013-09-11 17:38:41 [690] [INFO] Booting worker with pid: 690
http://0.0.0.0:5000
中打开我的应用程序:
17:38:46 web.1 | 2013-09-11 10:38:46 [690] [ERROR] Error handling request
17:38:46 web.1 | Traceback (most recent call last):
17:38:46 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 131, in handle_request
17:38:46 web.1 | respiter = self.wsgi(environ, resp.start_response)
17:38:46 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/dj_static.py", line 59, in __call__
17:38:46 web.1 | return self.application(environ, start_response)
17:38:46 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
17:38:46 web.1 | self.load_middleware()
17:38:46 web.1 | File "/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
17:38:46 web.1 | raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
17:38:46 web.1 | ImproperlyConfigured: Error importing middleware django.contrib.auth.middleware: "dlopen(/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @loader_path/../lib/libssl.1.0.0.dylib
17:38:46 web.1 | Referenced from: /Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/psycopg2/_psycopg.so
17:38:46 web.1 | Reason: image not found"
dj_database_url.config()
返回了:
{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'HOST': 'localhost',
'USER': 'dbuser',
'PASSWORD': 'dbpassword',
'PORT': 5000
}
mac-pol:~ oubiga$ pip install autoenv
肯尼思·雷茨(Kenneth Reitz)在这张
Cookbook中写道:
use_env() {
typeset venv
venv="$1"
if [[ "${VIRTUAL_ENV:t}" != "$venv" ]]; then
if workon | grep -q "$venv"; then
workon "$venv"
else
echo -n "Create virtualenv $venv now? (Yn) "
read answer
if [[ "$answer" == "Y" ]]; then
mkvirtualenv "$venv"
fi
fi
fi
}
$ foreman start
命令:
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
18:11:57 web.1 | started with pid 1104
18:11:57 web.1 | 2013-09-11 18:11:57 [1104] [INFO] Starting gunicorn 18.0
18:11:57 web.1 | 2013-09-11 18:11:57 [1104] [INFO] Listening at: http://0.0.0.0:5000 (1104)
18:11:57 web.1 | 2013-09-11 18:11:57 [1104] [INFO] Using worker: sync
18:11:57 web.1 | 2013-09-11 18:11:57 [1107] [INFO] Booting worker with pid: 1107
http://0.0.0.0:5000
:它起作用了!
^CSIGINT received
18:12:06 system | sending SIGTERM to all processes
18:12:06 web.1 | 2013-09-11 11:12:06 [1107] [INFO] Worker exiting (pid: 1107)
SIGTERM received
18:12:06 web.1 | 2013-09-11 18:12:06 [1104] [INFO] Handling signal: int
18:12:06 web.1 | 2013-09-11 18:12:06 [1104] [INFO] Shutting down: Master
18:12:06 web.1 | exited with code 0
dj_database_url.config()
再次返回一个空字典。
python manage.py runserver
命令感到好奇,并检查了一下。
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman run python hellodjango/manage.py runserver
Validating models...
0 errors found
September 11, 2013 - 18:42:37
Django version 1.5.2, using settings 'hellodjango.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
http://127.0.0.1:8000/
:它不起作用!
ImportError: No module named hellodjango.urls
。
ROOT_URLCONF = 'hellodjango.hellodjango.urls'
替换了settings.py文件中的
ROOT_URLCONF = 'hellodjango.urls'
,它终于起作用了。
dj_database_url.config()
返回了一个空字典。
dj_database_url.config()
有时返回完整填充的字典,有时却返回空的字典? 最佳答案
我也陷入了postgres的困境,这是我在settings.py中添加本地设置的方法:
DATABASES = {
'default': dj_database_url.config(default='postgres://<user>:<password>@<host>/<dbname>')
}
关于django - 在Heroku : Can I manually set environment variables in the . env文件上部署Django应用程序?我需要安装autoenv,heroku-config等工具吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753270/
我听说最好不要从您系统的 Perl 版本所在的 CPAN 安装模块。我知道如何使用命令行安装模块,我只是想知道是否有办法将 CPAN 与系统核心 Perl 分开。 我应该: 下载源代码并专门为这些模块
我听说最好不要从系统的 Perl 版本所在的 CPAN 安装模块。我知道如何使用命令行安装模块,我只是想知道是否有办法将 CPAN 与系统的核心 Perl 分开。 我应该: 下载源代码并专门为这些模块
单独安装 electron 与通过 electron-builder 安装有什么区别?我正在使用 React 构建一个 Electron 应用程序,并且已经找到了一些教程。它们安装 Electron
两者安装有什么区别?我按照安装页面上的说明在全局范围内安装了 webpack,然后我转到了入门指南,据说在那里可以在本地安装 webpack-cli。 CLI = Command Line Inter
我在 OS X Yosemite 上用 PHP 安装了默认的 Apache 服务器,安装了 pear,用 brew 安装了 Solr (brew install solr),现在我正在尝试使用 PEC
我解压并编译了 Ruby 2.1 并安装了几个支持工具。 但是当我安装了 libssl-dev 时,OpenSSL 不会安装。 我在支持 openssl 时遇到这个错误: make: *** No r
我在 android studio 2.3.1 和 gradle 3.2 中设计了 2 到 3 个应用程序。当我从它运行应用程序到任何设备或模拟器时,一切都工作正常。但是当我从构建文件夹中获取该 ap
我注意到我正在读一本书提到通过 apt-get 安装 numpy 和 opencv apt-get install python-numpy python-opencv 但我可以通过以下方式在 pip
我正在尝试在 Windows 8.1 上安装 ansicon。我提取了文件并达到了我需要调用 ansicon -i 的级别。当我在 cmd 中输入此内容并运行 python 脚本时效果很好,但是当我通
我有 linux MINT 17.3 Kernel 4.4.0-81 所有更新可用。 (由于不同的原因,我无法迁移到更新版本的 ubuntu/mint) 我已经通过 PPA 安装了 FFMPEG(不是
尝试在本地运行我的应用程序时出现错误: 我只在 chrome 浏览器中收到此错误。我尝试过不同的东西,但我不确定为什么它是 Chrome 特定的。 最佳答案 我怀疑这不是 Firebase 问题,而是
这是我第一次开发 AngularJS 应用程序并使用脚手架工具 Yeoman ( http://yeoman.io/ )。我想对我的一些图标使用 fontawesome ( http://fortaw
我知道您通常“应该”$ pip install 如果包没有 brew ,但如果有一个你想安装的 python 包,你可以使用 $ pip install或 $ brew install为了?例如,通过
我正在尝试通过 RVM 安装 Ruby 1.9.3。然而,当谈到安装 RubyGems 时,我得到了这个: curl: (22) The requested URL returned error: 4
我是真正提出问题的新手,但你去吧。 我一直在尝试按照安装指南添加 dnsname: https://github.com/containers/dnsname https://github.com/c
Studio更新至0.4.0 建筑产量为“需要1.8版Gradle”;将设置设置为1.8 bin目录; 建立 “要求1.9级”;将设置设置为1.9 bin; 建立 “要求1.8级” 啊。不知道该怎么做
我刚刚注意到 kernel.org 因维护而停机。是否有使用不同镜子的不同公式?或者我可以向 Homebrew 软件添加不同的来源(如 bundler ?)? 谢谢你的帮助! 最佳答案 快速解决方法:
当我运行时: peardev install phpunit/PHPUnit 我得到以下信息: No releases available for package "pear.phpunit.de/P
服务器操作系统为Fedora 24. 64bit。 我想安装 Git 2.6.6。 所以下载源码并安装。 此服务器离线。所以我不使用“yum”。 ./configure --prefix=/usr/l
我正在尝试在我自己的服务器(操作系统:Linux Ubuntu Server 12.04)上安装 OpenEdX,但我遇到了同样的错误。谁能帮帮我? TASK: [ insights | insta
我是一名优秀的程序员,十分优秀!