Heroku部署Django
一、安装必要的三方库
dj-database-url:帮助Django与Heroku使用的数据库进行通信 dj-static:帮助Django正确的管理静态文件 gunicorn:服务器软件,能够在在线环境中支持应用程序提供的服务 static3:与dj-static相同
二、创建包含三方库列表的文件requirements.txt
pip freeze > requirements.txt
项目根目录下自动生成如下文件:
dj-database-url==0.5.0 dj-static==0.0.6 gunicorn==19.9.0 static3==0.7.0 Django==2.0.8 Pillow==5.2.0 beautifulsoup4==4.6.3 psycopg2>=2.6.1
其中‘psycopg2>=2.6.1’为手动添加,Heroku使用postgre数据库。
加上django_heroku==0.3.1
三、指定python版本:项目根目录下创建runtime.txt
python-3.6.6
四、创建Procfile
Procfile告诉Heroku启动哪些进程,无扩展名,保存到项目根目录下。
web: gunicorn wsgi的目录名.wsgi --log-file -
五、配置文件同级目录下创建用于存储静态文件的目录:static,static下创建任意文件,文件内创建任意内容。
在Heroku上,Django搜集所有静态文件,把他们放在配置文件同级的static下,因为推送项目时空文件夹会被忽略,所以需要创建一个占位文件。
六、修改配置文件
import django_heroku django_heroku.settings(locals())
需要在requirements.txt加上django_heroku==0.3.1
os.getcwd():获取当前工作目录,在Heroku部署中,这个目录总是'/app',而在本地这个目录通常是项目根目录名。if条件确保仅当项目被部署到heroku时运行这个代码块。
# Heroku 设置 if os.getcwd() == '/app': import dj_database_url DATABASES = { 'default': dj_database_url.config(default='postgres://localhost') } # 让request.is_secure()承认X-Forwarded-Proto头 SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') # 只允许Heroku托管这个项目 ALLOWED_HOSTS = ["app名(后边提到).herokuapp.com"] DEBUG = False # 静态资源配置 STATIC_ROOT = 'staticfiles' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
七、修改wsgi.py:
Cling帮助正确的导入静态文件,在本地也使用。
# from django.core.wsgi import get_wsgi_application from dj_static import Cling # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "SQL_exercise.settings") application = Cling(get_wsgi_application())
八、创建git账户,在根目录下创建忽略文件.gitignore.txt
# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/
九、创建Heroku账户,部署项目
注意1:创建Heroku需要FQ,邮箱需要国外邮箱,qq不能用。
账户创建好之后,访问https://dashboard.heroku.com/apps/,新建app(app的名字将是你项目的url),然后按照Heroku的提示做就可以了。
注意2:在执行 git push heroku master 之前,先执行 heroku config:set DISABLE_COLLECTSTATIC=0 ,
在Heroku上建立数据库:
heroku run python manage.py migrate
创建超级用户(需要的话):
heroku run bash
python manage.py createsuperuser
或
heroku run python manage.py createsuperuser
查看日志
heroku logs --tail
十、将项目从Heroku上删除
heroku apps:destroy --app appname

浙公网安备 33010602011771号