Django 3 部署在Heroku上的詳細(xì)步驟

Deploy Django3 on heroku (CLI 命令行)

  1. 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è))

  2. Go to shell: 命令行:

heroku login
  1. 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的位置。

  1. 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
  1. 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自己是不支持的。

  1. 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
  1. 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)

  1. 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

  1. Add this to the ALLOWED_HOSTS in settings.py, 把這個(gè)訪問(wèn)域名添加到settings.py中的ALLOWED_HOSTS中
ALLOWED_HOSTS = ['myappname.herokuapp.com'] 

  1. 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
  1. 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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容