1. install Git
https://git-scm.com/
2. install Heroku Cli
https://devcenter.heroku.com/categories/command-line
3. 申請 Heroku 帳號
Heroku | 搭配 Git 在 Heroku 上部署網站的手把手教學
5.準備好上傳網頁
5.1下載免費主題
https://github.com/thecodinghouse/mezzanine-themes
5.2下載並解壓縮整理好備用
1. 安裝虛擬工作環境
mkvirtualenv your-project-name
2. 安裝 Mezzanine
pip install mezzanine
3. 安裝購物車套件(選配, 有需要者裝)
pip install –U cartridge
4. 建立 Mezzanine 專案
mezzanine-project your-project-name
5. 切換至內容網站的工作目錄 , 並新增檔案 Procfile
5.1 cd your-project-name
5.2 建立新檔案 Procfile
內容如下(注意 your-project-name 是替換成自己的專案名稱。)
web: gunicorn --pythonpath your-project-name your-project-name.wsgi
完成時存檔時, 請注意
Procfile - Procfile - P要大寫,
不要有副檔名
6. 編輯 wsgi.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | """ WSGI config for mymezzanine project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application from mezzanine.utils.conf import real_project_name from dj_static import Cling os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.settings" % real_project_name("mymezzanine")) application = Cling(get_wsgi_application()) |
7. 修訂 settings.py,
7.1 時區 TIME_ZONE = 'Asia/Taipei'
7.2 LANGUAGE_CODE = "zh-tw"
7.3多語言新增 ('zh-tw', _('繁體中文')), )
7.4 USE_I18N = True
7.5 最下方加入三行
SECRET_KEY = "由 local_settings.py 複製而來"
ALLOWED_HOSTS = ["*"]
STATIC_ROOT = 'static'
# system time zone. TIME_ZONE = 'Asia/Taipei' (約104列) # If you set this to True, Django will use timezone-aware datetimes. USE_TZ = True # Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = "zh-tw" (約111列) # Supported languages LANGUAGES = ( ('en', _('English')), ('zh-tw', _('繁體中文')), (約116列) )
# A boolean that turns on/off debug mode. When set to ``True``, stack traces
# are displayed for error pages. Should always be set to ``False`` in
# production. Best set to ``True`` in local_settings.py
DEBUG = False
# Whether a user's session cookie expires when the Web browser is closed.
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True (約 131列, 將False 改為 True)
.
.
try: from mezzanine.utils.conf import set_dynamic_settings except ImportError: pass else: set_dynamic_settings(globals()) #最下方加入
SECRET_KEY = "o^2ywlr-(!28f8+635em#kxugxw*" (本列由local_settings.py複製)
ALLOWED_HOSTS = ["*"]
STATIC_ROOT = 'static'
8. 套用免費樣板(template)
8.1 選擇使用 moderna 樣版目錄複製到 your-project-name 目錄內(與manage.py同目錄內)
8.2 settings.py設定moderna
8.2.1 設定template的工作目錄
8.2.2 INSTALLED_APP 加入 moderna
.
.
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
os.path.join(PROJECT_ROOT, "moderna/templates")
#os.path.join(PROJECT_ROOT,"templates")
],
.
.
INSTALLED_APPS = (
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.redirects",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.sitemaps",
"django.contrib.staticfiles",
"moderna",
"mezzanine.boot",
"mezzanine.conf",
.
.
9. 新增 production_settings.py, 設定 Heroku 佈署命令,儲存在
your-project-name/your-project-name/production_settings.py
# Import all default settings. from .settings import * import dj_database_url DATABASES = { 'default': dj_database_url.config(), } # Static asset configuration. STATIC_ROOT = 'static' # Honor the 'X-Forwarded-Proto' header for request.is_secure(). SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') # Allow all host headers. ALLOWED_HOSTS = ['*'] # Turn off DEBUG mode. DEBUG = False
10. 執行收集靜態檔案
python manage.py collectstatic
11. 編輯 .gitignore
要把 static 目錄下的檔案上傳,必須要.gitignore 檔案內的/static 刪除
*.pyc *.pyo *.db .DS_Store .coverage local_settings.py/static刪除此行
12. 安裝 django_heroku 套件
pip install django_heroku
13. 安裝 gunicorn 套件
pip install gunicorn
14. 安裝 dj_staic 套件
pip install dj_static
15. 安裝
pip install psycopg2-binary
16. 設定初始化資料庫
python manage.py createdb
Creating default account....
username:admin
Email: xxx@gmail.com
passward: 1234
17. 製作需求套件的檔案
pip freeze >requirements.txt
18. 工作目錄下執行, , 本地端檢視網站是否可正常執行
python manage.py runserver
19. 執行 git 安裝git之前就開了cmd視窗,請重新開啟它。)
git init
git add .
git commit –m “initial commit”
第一次使用時
git config --global user.email "xxx@gmail.com"
git config --global user.name "xxx"
20. 登入 Heroku (command 視窗輸入, 會在瀏覽器開啟登入頁面)
heroku login
按下 enter 鍵
點擊 login 後,回到 cmd 等待
21. 建立網站
heroku create your-project-name
22 到 heroku your-project-name設定 buildpack 到 Python
23. 設定初始化
heroku config:set DISABLE_COLLECTSTATIC=1
24. 上傳到網站
git push heroku master
25. 設定 (your-project-name修改為自己的專案名稱)
heroku config:set DJANGO_SETTINGS_MODULE=your-project-name.production_settings
26. 第一次佈署
heroku ps:scale web=1
27. 初始化資料庫
heroku run python manage.py createdb
28. 建立使用者帳號
heroku run python manage.py createsuperuser
admin 1234
29. 開啟網站 (自動開啟瀏覽器)
heroku open
30. 開啟失敗, 檢視 log 檔除錯
heroku logs
31重新上傳
git add.
git commit -m "update" (""內文字可自由更換)
git push heroku master
heroku open