我发现要做什么与其找教程,还不如直接看官方提供的英文文档.
链接:https://devcenter.heroku.com/articles/getting-started-with-django
我大致翻译一下:
1.首先准备的东西
Heroku Toolbelt,python,Virtualenv, Postgres数据库.但是我部署的项目的没有针对postgres数据库,所以这地方没有测试.
2.在适当位置执行以下命令,创建一个目录
mkdir hellodjango && cd hellodjango
3.在该目录下执行virtualenv venv,最好带上参数--no-site-packages,不依赖网络的包
virtualenv venv New python executable in venv/bin/python Installing setuptools, pip...done
4.激活环境
source venv/bin/activate
- Django (the web framework)
- Gunicorn (WSGI server)
- dj-database-url (a Django configuration helper)
- dj-static (a Django static file server)
pip install django-toolbelt
6.新建一个项目,注意小数点(.)不要忘了,在当前目录下创子目录
django-admin.py startproject hellodjango .
7.写一个Profile文件,在manage.py同目录中,内容如下
web: gunicorn hellodjango.wsgi --log-file -
8.启动看看.装了Heroku Toolbelt之后会有的foreman命令.启动成功,可以打开浏览器看看效果,此时看到的效果会和上传后一致.会发现没有加载css/js等静态文件
foreman start 2013-04-03 16:11:22 [8469] [INFO] Starting gunicorn 0.17.2 2013-04-03 16:11:22 [8469] [INFO] Listening at: http://127.0.0.1:8000 (8469)
9.生成requirements.txt文件
pip freeze > requirements.txt
10.设置settings.py,官方说要使用dj_datababe_url模块自动找寻 Postgres数据库,我没有用,所以就没加上
# Parse database configuration from $DATABASE_URL import dj_database_url DATABASES['default'] = dj_database_url.config() # Honor the 'X-Forwarded-Proto' header for request.is_secure() SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')11.设置wsgi.py文件,这样才可以识别到静态文件css/js等
from django.core.wsgi import get_wsgi_application from dj_static import Cling application = Cling(get_wsgi_application())12. (.gitignore)文件的编写按需要加入以下内容.git时会忽略掉的内容
venv *.pyc staticfiles
13.用git上传到heroku,可以参考我发布的webpy部署到heroku的教程
好啦,更加具体的请参考官方文档.
可能会遇到的问题:
装django-toolbelt会遇到问题,这个问题我在windows平台下没有得到很好的解决,如果谁有解决方案,可以留言给我,感激不尽.
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATHor specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
解决办法
sudo apt-get install libpq-dev python-dev