Django 3 部署在Heroku上的详细步骤

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项目(假设你这些都做好了,你应该可以直接访问http://127.0.0.1:8000/ 看到项目首页)

  2. Go to shell: 命令行:

heroku login
  1. create Procfile in the porject root directory 在根目录下创建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就是你的项目的名字,告诉heroku wsgi.py的位置。

  1. Add a runtime.txt file in the project root directory, open it and write your python version 然后添加python的版本到runtime.txt文件中,置于项目根目录下:
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.
安装原因详细可以看https://devcenter.heroku.com/articles/django-app-configuration
比如Heroku是推荐用WhiteNoise来做静态文件的呈现的(发行版本下),Django自己是不支持的。

  1. Add a requirements.txt file to the root directory to tell Heroku your application's module dependencies 然后在根目录下添加requirements.txt 告诉heroku包的依赖关系,可以使用命令如下:
pip freeze > requirements.txt

Should look like this 这个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怎么去处理静态文件
    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 如果用数据库添加
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'
    在终端创建Heroku App, 假设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, 把这个访问域名添加到settings.py中的ALLOWED_HOSTS中
ALLOWED_HOSTS = ['myappname.herokuapp.com'] 

  1. Initialise git and connect to Heroku Git Remote with the following commands 然后初始化本地的git, push到远程的heroku repo中:
git init

heroku git:remote -a passwordgeneratorapp

git add .

git commit -m "Initial commit"

git push heroku master
  1. Migrate your database 如果需要迁移数据库:
heroku run python manage.py migrate

At this stage, we can visit myappname.herokuapp.com
做完这些,应该就可以访问你host在heroku上的app了:
http://myappname.herokuapp.com

你可能感兴趣的:(Django 3 部署在Heroku上的详细步骤)