8

介绍:

  • 我正在关注Heroku 上的 Django 入门快速入门指南。

  • 我打算应用关于使用 virtualenvs 和项目的 Django 书籍哲学的两个勺子。Audrey RoyDaniel Greenfeld(作者)喜欢将所有环境放在一个目录中,将所有项目放在另一个目录中。

  • 我还打算应用Django 的两勺书哲学,将django-admin.py startproject管理命令生成的内容放在另一个目录中,该目录用作 git 存储库根目录(也称为三层方法)。

最高层的布局:

repository_root/
    django_project_root/
        configuration_root/


本地:

OS X 10.8.4
pip==1.4.1
virtualenv==1.10.1
virtualenvwrapper==4.1.1
wsgiref==0.1.2
.bashrc 包含:

export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/Projects

〜/环境
〜/项目


在 hellodjango_venv 内部:

Django==1.5.2
dj-database-url==0.2.2
dj-static==0.0.5
django-toolbelt==0.0.1
gunicorn==18.0
psycopg2==2.5.1
static==0.4


从终端:

mac-pol:~ oubiga$ mkdir -p Projects/hellodjango_rep/hellodjango
mac-pol:~ oubiga$ cd Projects/hellodjango_rep/hellodjango
mac-pol:hellodjango oubiga$ mkvirtualenv hellodjango_venv
New python executable in hellodjango_venv/bin/python2.7
Also creating executable in hellodjango_venv/bin/python
Installing Setuptools..................................

...

..................................................done.
(hellodjango_venv)mac-pol:hellodjango oubiga$ pip install django-toolbelt

...

Successfully installed django-toolbelt django psycopg2 gunicorn dj-database-url dj-static static
Cleaning up...
(hellodjango_venv)mac-pol:hellodjango oubiga$ django-admin.py startproject hellodjango .
(hellodjango_venv)mac-pol:hellodjango oubiga$ touch Procfile && open Procfile

编辑过程文件:

web: gunicorn hellodjango.wsgi

启动流程:

(hellodjango_venv)mac-pol:hellodjango oubiga$ foreman start
01:30:37 web.1  | started with pid 638
01:30:37 web.1  | 2013-09-04 01:30:37 [638] [INFO] Starting gunicorn 18.0
01:30:37 web.1  | 2013-09-04 01:30:37 [638] [INFO] Listening at: http://0.0.0.0:5000 (638)
01:30:37 web.1  | 2013-09-04 01:30:37 [638] [INFO] Using worker: sync
01:30:37 web.1  | 2013-09-04 01:30:37 [641] [INFO] Booting worker with pid: 641
CTRL+C

到目前为止,一切都很好。

目前我的项目树是:

~/Projects/    
    hellodjango_rep/
        hellodjango/ cwd
            manage.py
            Procfile
            hellodjango/
                __init__.py
                settings/
                urls.py
                wsgi.py

我的 Virtualenvs 树是:

~/Envs/
    get_env_details
    initialize
    postactivate
    postdeactivate
    postmkproject
    postmkvirtualenv
    postrmproject
    postrmvirtualenv
    preactivate
    predeactivate
    premkproject
    premkvirtualenv
    prermproject
    prermvirtualenv
    hellodjango_venv/
        bin/
        include/
        lib/

但是,你还记得三层方法吗?我怎样才能实现它?

我很确定 hellodjango_rep/ 稍后将至少包含:.git、.gitignore 和 requirements.txt


研究:

在#django IRC 频道上,Jacob Kaplan-Moss回答:

... Procfile 需要位于您的 git repo 的顶层...

Heroku 的工程师Neil Middleton在 SO 中回答了这个问题“Procfile 在 Heroku 中声明类型 -> (none)”:

...您的 Procfile 需要位于推送到 Heroku 的 git 存储库的根目录中...

来自 Heroku 开发中心:

...进程类型是通过放置在应用程序根目录中的名为 Procfile 的文件声明的...


作为第一次尝试:

我将 Procfile 向上移动一个文件夹级别。

(hellodjango_venv)mac-pol:hellodjango oubiga$ cd ..

目前我的项目树是:

~/Projects/    
    hellodjango_rep/ cwd
        Procfile
        hellodjango/
            manage.py
            hellodjango/
                __init__.py
                settings/
                urls.py
                wsgi.py

我再次开始这个过程:

(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start

我会得到ImportError: No module named hellodjango.wsgi


调试:

ImportError 似乎来自/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/gunicorn/util.py

def import_app(module):
    parts = module.split(":", 1)
    if len(parts) == 1:
        module, obj = module, "application"
    else:
        module, obj = parts[0], parts[1]

    try:
        __import__(module)  # <-- line 354
    except ImportError:
        if module.endswith(".py") and os.path.exists(module):
            raise ImportError("Failed to find application, did "
                "you mean '%s:%s'?" % (module.rsplit(".", 1)[0], obj))
        else:
            raise

...

没有名为 hellodjango.wsgi 的模块


作为第二次尝试:

Procfile 回到上一个级别。Procfile 和 manage.py 再次属于一起。

(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start

我会明白ERROR: Procfile does not exist.为什么会这样。


假设:

来自 Django 文档:

...manage.py 在每个 Django 项目中自动创建。manage.py 是 django-admin.py 的一个瘦包装器,它在委托给 django-admin.py 之前为您处理两件事:
- 它将您的项目包放在 sys.path 上。
- 它设置 DJANGO_SETTINGS_MODULE 环境变量,使其指向您项目的 settings.py 文件...

我不确定问题是否与此有关。


所以:

是否可以在不同的文件夹级别有一个 Procfile 和一个 manage.py 文件?
这种方法有什么问题?


先感谢您。

4

2 回答 2

5

首先,我不得不说,我没有进行如此广泛的研究,并且已经使用了 Heroku 大约 3 次。但:

(您的第二次尝试):

Procfile 确实应该在顶级目录中。如果你把 Procfile 移得更深, gunicorn 就找不到了。

(您的第一次尝试):

并且将您的 Procfile 放在顶级目录中,您应该说明 wsgi 的路径。这是不可能的。所以你应该在第一个“hellodjango”目录中创建 __init__.py 文件,将其标记为“模块”。然后你应该将 Procfile 中的路径更改为:hellodjango.hellodjango.wsgi

希望这可以帮助。

于 2013-09-04T20:34:33.890 回答
5

我使用Two Scoops布局遇到了这个问题,我的解决方案是保持标准布局(Procfilein repository_rootmanage.pyin project_root)并在运行 gunicorn 之前制作Procfile一个将目录更改为的脚本。project_root

例如,在Procfile

web: sh -c 'cd ./mysite/ && exec gunicorn mysite.wsgi --log-file -'

您同样可以Procfile调用外部脚本:

web: bash scripts/heroku_run

我更喜欢添加额外的模块路径,因为它可能会弄乱settings.pyandurls.py等中的导入。

于 2014-08-13T11:28:08.233 回答