Deploy Django3 on heroku (CLI 命令行)
Set up your own django 3 project locally. Visit http://127.0.0.1:8000/ on the browser. If your setup is correct, you should see the Django welcome page.先初始化你的本地的django 3項(xiàng)目(假設(shè)你這些都做好了,你應(yīng)該可以直接訪問(wèn)http://127.0.0.1:8000/ 看到項(xiàng)目首頁(yè))
Go to shell: 命令行:
heroku login
- create Procfile in the porject root directory 在根目錄下創(chuàng)建Procfile文件
touch Procfile
Add this to the file: 然后添加如下一行:
web: gunicorn yourprojectsname.wsgi --log-file -
The yourprojectsname suggests where to find the location of wsgi.py 其中yourprojectsname就是你的項(xiàng)目的名字,告訴heroku wsgi.py的位置。
- Add a runtime.txt file in the project root directory, open it and write your python version 然后添加python的版本到runtime.txt文件中,置于項(xiàng)目根目錄下:
python-3.7.3
- Install the following packages in the environments 安裝以下的包:
pip install gunicorn dj-database-url whitenoise psycopg2
The reason to install these modules is here:
https://devcenter.heroku.com/articles/django-app-configuration
For example,Heroku recommends the use of WhiteNoise (a Django package) to serve static files in production, since Django does not support serving static files in production, by default.
安裝原因詳細(xì)可以看https://devcenter.heroku.com/articles/django-app-configuration
比如Heroku是推薦用WhiteNoise來(lái)做靜態(tài)文件的呈現(xiàn)的(發(fā)行版本下),Django自己是不支持的。
- Add a requirements.txt file to the root directory to tell Heroku your application's module dependencies 然后在根目錄下添加requirements.txt 告訴heroku包的依賴關(guān)系,可以使用命令如下:
pip freeze > requirements.txt
Should look like this 這個(gè)txt文件可以看上去如下:
dj-database-url==xxx
Django==3.x
gunicorn==xxx
psycopg2==xxx
pytz==xxx
whitenoise==xxx
- This step is to tell Heroku how to set up the static assets and what folders it needs to create on the server. 這一步就是要配置settings.py了,主要是告訴Heroku怎么去處理靜態(tài)文件
Go to the settings.py file and add:
DEBUG = False # change to false cuz now we will deploy
....
# add whitenoise middleware to the top 添加whitenoise到middleware
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
...
...
]
# in the bottom of the file 文件最底部添加
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# if you will use database in the project 如果用數(shù)據(jù)庫(kù)添加
import dj_database_url
prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)
- Create App in Heroku from terminal, e.g. we may create the app name 'myappname'
在終端創(chuàng)建Heroku App, 假設(shè)app名稱為'myappname'
heroku create myappname
This allow me to see my domain name of the app.
這樣你就可以看到你的app在heroku上的域名:http://myappname.herokuapp.com
- Add this to the ALLOWED_HOSTS in settings.py, 把這個(gè)訪問(wèn)域名添加到settings.py中的ALLOWED_HOSTS中
ALLOWED_HOSTS = ['myappname.herokuapp.com']
- Initialise git and connect to Heroku Git Remote with the following commands 然后初始化本地的git, push到遠(yuǎn)程的heroku repo中:
git init
heroku git:remote -a passwordgeneratorapp
git add .
git commit -m "Initial commit"
git push heroku master
- Migrate your database 如果需要遷移數(shù)據(jù)庫(kù):
heroku run python manage.py migrate
At this stage, we can visit myappname.herokuapp.com
做完這些,應(yīng)該就可以訪問(wèn)你host在heroku上的app了:
http://myappname.herokuapp.com