{"id":20619758,"url":"https://github.com/twtrubiks/deploying_django_to_heroku_tutorial","last_synced_at":"2025-08-03T03:38:16.021Z","repository":{"id":84518913,"uuid":"92664784","full_name":"twtrubiks/Deploying_Django_To_Heroku_Tutorial","owner":"twtrubiks","description":"Deploying a Django App To Heroku Tutorial ","archived":false,"fork":false,"pushed_at":"2022-06-29T03:29:44.000Z","size":708,"stargazers_count":22,"open_issues_count":1,"forks_count":11,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T12:16:28.355Z","etag":null,"topics":["deploy","django","heroku","python","tutorial"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twtrubiks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-28T14:03:08.000Z","updated_at":"2023-01-10T14:32:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"4e0c33b6-db0a-4357-8db8-d816f68c14f6","html_url":"https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/twtrubiks/Deploying_Django_To_Heroku_Tutorial","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_Tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_Tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_Tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_Tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDeploying_Django_To_Heroku_Tutorial/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261248990,"owners_count":23130489,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deploy","django","heroku","python","tutorial"],"created_at":"2024-11-16T12:12:27.438Z","updated_at":"2025-06-22T06:34:27.193Z","avatar_url":"https://github.com/twtrubiks.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deploying_Django_To_Heroku_Tutorial\n\nDeploying a Django App To Heroku Tutorial\n\n* [線上 Demo 網站](https://deploy-django-twtrubiks.herokuapp.com/api/images/)\n\n後端有一個自動爬蟲的程式去抓圖片，可參考 [auto_crawler_ptt_beauty_image](https://github.com/twtrubiks/auto_crawler_ptt_beauty_image) 。\n\n本專案將教大家如何將自己的 [Django](https://www.djangoproject.com/) 佈署到\n[Heroku](https://dashboard.heroku.com/)\n\n[Heroku](https://dashboard.heroku.com/) 免費版本\n\n* 可以創造 5個 app。\n* 24小時一定要休息6小時的規定。\n* 支援很多種程式語言。\n* 有SSL ( https ) 。\n\n更多說明請參考 [Heroku](https://dashboard.heroku.com/)\n\n詳細的步驟可參考 [Deploying-Flask-To-Heroku](https://github.com/twtrubiks/Deploying-Flask-To-Heroku) ，\n\n下面將只介紹佈署 [Django](https://www.djangoproject.com/) 要注意的步驟。\n\n## 教學\n\n我的 project_name 為 ptt_beauty_images\n\n***設定 [Procfile](https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial/blob/master/Procfile)***\n\n```python\nweb: gunicorn ptt_beauty_images.wsgi\n```\n\n更多說明請參考\n\n[https://devcenter.heroku.com/articles/django-app-configuration#the-basics](https://devcenter.heroku.com/articles/django-app-configuration#the-basics)\n\n## 建議使用 WhiteNoise 佈署\n\n***設定 Collectstatic***\n\nDisabling Collectstatic\n\u003e heroku config:set DISABLE_COLLECTSTATIC=1\n\n可參考 [https://devcenter.heroku.com/articles/django-assets#collectstatic-during-builds](https://devcenter.heroku.com/articles/django-assets#collectstatic-during-builds)\n\n```cmd\npip3 install whitenoise\n```\n\n這樣靜態檔案才會正常顯示.\n\n詳細說明可參考 [Using WhiteNoise with Django](https://whitenoise.evans.io/en/stable/django.html)\n\n在 [settings.py](https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial/blob/master/ptt_beauty_images/settings.py) 中加入以下東西,\n\n***設定 ALLOWED_HOSTS***\n\n```python\nALLOWED_HOSTS = ['*']\n```\n\n記得把 DEBUG 修改為 `False`\n\n```python\nDEBUG = False\n```\n\n設定 STATIC_ROOT\n\n```python\nSTATIC_ROOT = BASE_DIR / \"staticfiles\"\n```\n\n設定 WhiteNoise 到 MIDDLEWARE\n\n```python\nMIDDLEWARE = [\n    # ...\n    \"django.middleware.security.SecurityMiddleware\",\n    \"whitenoise.middleware.WhiteNoiseMiddleware\",\n    # ...\n]\n```\n\nAdd compression\n\n```python\nSTATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'\n```\n\n最後執行 `python3 manage.py collectstatic`\n\n如果沒有任何錯誤, 再將產生出來的東西一起 push 到 Heroku 上.\n\n***設定 DATABASE***\n\n[settings.py](https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial/blob/master/ptt_beauty_images/settings.py)\n\n我使用 Heroku Postgres ，\n\n詳細教學可參考 [如何在 heroku 上使用 database](https://github.com/twtrubiks/Deploying-Flask-To-Heroku#%E5%A6%82%E4%BD%95%E5%9C%A8-heroku-%E4%B8%8A%E4%BD%BF%E7%94%A8-database)\n\n```python\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql_psycopg2',\n        'NAME': os.environ.get('DATABASE_NAME'),\n        'USER': os.environ.get('DATABASE_USER'),\n        'PASSWORD': os.environ.get('DATABASE_PASSWORD'),\n        'HOST': os.environ.get('DATABASE_HOST'),\n        'PORT': os.environ.get('DATABASE_PORT'),\n    }\n}\n```\n\n請在自己 [Heroku](https://dashboard.heroku.com/) 的 Config Variables 設定 DATABASE 連線字串\n\n![](http://i.imgur.com/KsQyZ2f.png)\n\n## Django multiple-databases\n\n順便介紹在  [Django](https://www.djangoproject.com/) 中的 multiple-databases，\n\n使用方法， 將 [settings.py](https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial/blob/master/ptt_beauty_images/settings.py) 裡的 DATABASES 修改成如下\n\n```python\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql_psycopg2',\n        'NAME': os.environ.get('DATABASE_NAME'),\n        'USER': os.environ.get('DATABASE_USER'),\n        'PASSWORD': os.environ.get('DATABASE_PASSWORD'),\n        'HOST': os.environ.get('DATABASE_HOST'),\n        'PORT': os.environ.get('DATABASE_PORT'),\n    },\n    'db2': {\n        'ENGINE': 'django.db.backends.postgresql_psycopg2',\n        'NAME': os.environ.get('DATABASE_NAME_2'),\n        'USER': os.environ.get('DATABASE_USER_2'),\n        'PASSWORD': os.environ.get('DATABASE_PASSWORD_2'),\n        'HOST': os.environ.get('DATABASE_HOST_2'),\n        'PORT': os.environ.get('DATABASE_PORT_2'),\n    }\n}\n```\n\n這樣就代表你有兩個 DATABASES，更多資料可參考 [Django multi-db](https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#defining-your-databases)\n\n如果要指定 DATABASES 也非常容易，\n\n假設我今天要用 'default' 這個 DB ，可以寫這樣\n\n```python\nImage.objects.using('default').all()\n```\n\n假設我今天要用 'db2' 這個 DB，可以寫這樣\n\n```python\nImage.objects.using('db2').all()\n```\n\n有沒有發現，其實就是 using('database name') 這樣，非常簡單\n\n更多資料可參考 [Django manually-selecting-a-database](https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#manually-selecting-a-database)\n\n## 更新 heroku stack 指令\n\n指令如下\n\n```cmd\nheroku stack:set heroku-22 -a \u003capp name\u003e\n```\n\n之後再 commit push 一次就會自動 migrate 到新的 stack.\n\n## 執行環境\n\n* Python 3.9\n\n## Reference\n\n* [django-app-configuration](https://devcenter.heroku.com/articles/django-app-configuration)\n* [heroku-django-template](https://github.com/heroku/heroku-django-template)\n* [lazyload](https://github.com/verlok/lazyload)\n* [bootstrap-sweetalert](https://github.com/lipis/bootstrap-sweetalert)\n\n## Donation\n\n文章都是我自己研究內化後原創，如果有幫助到您，也想鼓勵我的話，歡迎請我喝一杯咖啡:laughing:\n\n![alt tag](https://i.imgur.com/LRct9xa.png)\n\n[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8)\n\n## License\n\nMIT license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdeploying_django_to_heroku_tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fdeploying_django_to_heroku_tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdeploying_django_to_heroku_tutorial/lists"}