Heroku 上部署Django 项目
【摘要】 Python Django 项目部署到Heroku 服务器上。
零、搭建项目运行环境。
1.创建显目目录
2、安装virtualenv 环境
3、启动env环境
4、检查所在的环境
一、创建Django 项目存放的文件目录,并cd 到改目录下。
二、使用django-admin创建Django项目。
cd 进入Django项目目录下。
三、创建app应用。
四、使用VS Code 打开项目
1、创建V模块,返回内容给请求的页面。
2、在urls文件中配置对应关系。
3、使用cmd命令安装gunicorn、 psycopg2 、whitenoise
4、使用cmd命令创建requirements.txt文件
Run command in cmd (cmd in directory where manage.py file) :
# { start
pip freeze > requirements.txt
# end }
5、创建Procfile文件
Create file Procfile and add content :
# { start
web: gunicorn <your_project_name>.wsgi
# end }
6、创建runtime.txt文件
Create file runtime.txt file and add below content :
# { start
<your complete version of python in mycase "python-2.7.12" >
# end }
7、修改settings.py 文件
In myproject/settings.py file add below content :
# { start
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# end }
8、修改wsgi.py 文件
In wsgi.py file add below content :
# { start
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
# end }
9、修改settings.py static 文件文件目录。
In settings.py file add below content :
# { start
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# end }
10、创建static目录,然后在创建一个空文件.keep。
Create folder name "static" in where manage.py directory
-> place your all static files in "static/" folder
-> if you have not any static file then add empty file in "static/" folder name it anything
that you want (let say ".keep") so that git track it otherwise you will get error
while deploying :)
11、git 命令提交文件
commit the myproject directory (git must be installed in your PC):
Run commands in myproject directory :
#{start
git init
git add .
git status
git commit -m "initial commit"
# end }
12、部署到heroku服务器上
login in heroku(heroku toolblet must be installed in your PC):
Run commands in myproject directory :
#{start
heroku login
heroku create <your website name(let say we take "shuboy2020")>
git push heroku master
heroku open
# end }
13、安装django-heroku:
pip install django-heroku
14、将以下import
语句添加到的顶部settings.py
:
import django_heroku
15、然后将以下内容添加到的底部settings.py
:
# Activate
Django-Heroku.django_heroku.settings(locals())
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)