[英]Install and Deploy Django app on Heroku
我經常忘記步驟,希望有一個關於在 Heroku 上部署 django 項目的快速指導指南。
如何在 Heroku 上安裝和部署 Django 應用程序?
我已經發布了對我有用的步驟的分步答案。
要快速安裝,請查看其他答案。
任何你看到這些的地方,用你的真實姓名替換!!!
如果您沒有virtualenv
,則需要獲取它。 它將允許您為每個項目單獨安裝軟件:
pip install virtualenv
然后我們創建我們的項目:
cd ~
mkdir PROJECT_WRAPPER && cd PROJECT_WRAPPER
virtualenv venv
現在您有一個專用文件夾,其中將包含獨立安裝和 python、django 等版本。
我們通過以下方式激活並開始項目工作:
source venv/bin/activate
在我們繼續之前,我們將安裝 postgres.app。 從以下網址獲取: http : //postgresapp.com/
安裝。
我們現在將使用它連接我們的環境:
PATH=/Applications/Postgres.app/Contents/MacOS/bin/:$PATH
現在我們需要安裝以下東西:
因此,要創建 requirements.txt 文件,我們將從我的 git 存儲庫中准備好它:
clone https://raw2.github.com/mgpepe/django-heroku-15/master/requirements.txt -o requirements.txt
現在使用一個命令,我們將從我們的 requirements.txt 安裝所有內容:
pip install -r requirements.txt
太好了,現在我們可以通過以下方式驗證我們是否擁有 django:
python -c "import django; print(django.get_version())"
讓我們用這一行開始項目,不要忘記最后的點:
django-admin.py startproject DJANGO_PROJECT .
現在,如果您鍵入ls
您應該會看到一個包含您的項目名稱的文件夾,其中包含您的 Django 項目。
要查看是否一切正常,請運行:
python manage.py runserver
運行 Postgres 應用程序。
創建一個數據庫(我使用了我的 osx 用戶名):
createdb YOUR_DATABASE_NAME --owner=YOUR_OSX_USERNAME
將DATABASES
更改為如下所示:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'YOUR_DATABASE_NAME',
'USER': 'YOUR_OSX_USERNAME',
'PASSWORD': 'YOUR_DATABASE_PASSWORD', #might be empty string ''
'HOST': '127.0.0.1',
# 'PORT': '5432',
}
}
並且讓我們連接南遷移。 您的INSTALLED_APPS
應如下所示:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
)
將SECRET_KEY
變量更改為其他內容。
現在,如果一切正常,您應該能夠創建第一個表:
python manage.py syncdb
現在在你的項目中制作你的第一個應用程序
python manage.py startapp DJANGO_APP
在文件中: ~/PROJECT_WRAPPER/DJANGO_PROJECT/settings.py
將DJANGO_APP
應用程序添加到變量INSTALLED_APPS
的列表中。 應該是這樣的:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'DJANGO_APP',
)
在設置文件中添加以下行:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
為了使模板組織得井井有條,我們將把 base.html 復制到一個文件夾中,並將其余模板復制到應用程序本身中:
cd ~/PROJECT_WRAPPER/
mkdir templates
curl https://raw2.github.com/mgpepe/django-heroku-15/master/templates/base.html -o base.html
現在剩下的模板:
cd ~/PROJECT_WRAPPER/DJANGO_APP/
mkdir templates && cd templates
mkdir DJANGO_APP
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/changepass.html -o changepass.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/forgot_pass.html -o forgot_pass.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/home.html -o home.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/login.html -o login.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/logout.html -o logout.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/registration.html -o registration.html
curl https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/templates/DjMainApp/splash.html -o splash.html
由於最近流行使用電子郵件而不是用戶名,我們也將這樣做。
*注意:如果您決定不使用它,您可以跳過這一步,但您必須編輯視圖和模板以使用用戶名而不是電子郵件。 *
在設置中添加以下行:
AUTHENTICATION_BACKENDS = (DJANGO_PROJECT.backends.EmailAuthBackend’,)
然后將文件 backends.py 復制到我們的項目目錄中:
cd ~/PROJECT_WRAPPER/DJANGO_PROJECT/
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/backends.py -o backends.py
您可以使用 Foreman 模擬在您的計算機上運行的 heroku。 讓我們創建最簡單的配置文件:
cd ~/PROJECT_WRAPPER
echo "web: gunicorn DJANGO_PROJECT.wsgi" > Procfile
foreman start
既然您看到它正常工作且沒有錯誤,請使用CTRL+C
停止它
在底部的設置中添加:
# HEROKU
###########################
# Parse database configuration from $DATABASE_URL
if os.environ.has_key('DATABASE_URL'):
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# 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 = ['*']
在您的DJANGO_PROJECT/wsgi.py
文件中,將以下內容添加到底部:
from dj_static import Cling
application = Cling(get_wsgi_application())
理想情況下,您可以從 Amazon 或類似的地方提供靜態文件。 但是對於簡單的站點,您可以使用 Django。 設置它需要您將其附加到設置文件中:
# HEROKU STATIC ASSETS CONFIGURATION
################################
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
並將所有靜態文件放在特定文件夾中。 首先轉到您的項目文件夾,例如:
cd ~/PROJECT_WRAPPER/DJANGO_PROJECT/
現在您可以復制/粘貼其余部分:
mkdir static && cd static
mkdir css && cd css
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/static/css/bootstrap.min.css -o bootstrap.min.css
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/static/css/styles.css -o styles.css
cd ..
mkdir js && cd js
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjangoHerokuIn15/static/js/bootstrap.min.js -o bootstrap.min.js
cd ..
mkdir img && cd img
在最后一個文件夾中,您將放置所需的所有圖像。
在 urls.py 中,在 'example' 之前復制這些行:
url(r'^$', "pmfmain.views.splash", name="splash"),
url(r'^login$', "pmfmain.views.login_view", name="login"),
url(r'^signup$', "pmfmain.views.register", name="signup"),
url(r'^forgot$', "pmfmain.views.forgot_pass", name="forgotmypass"),
url(r'^logout$', "pmfmain.views.logout_user", name="logout"),
url(r'^dashboard$', "pmfmain.views.home", name="home”),
然后將 views.py 從我的 github 存儲庫復制到您的 DJANGO_PROJECT 文件夾:
cd ~/PROJECT_WRAPPER/DJANGO_APP/
rm views.py
clone https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/views.py -o views.py
在整個views.py
查找並用您真實的 DJANGO_APP 名稱替換DjMainApp
克隆https://raw2.github.com/mgpepe/django-heroku-15/master/DjMainApp/forms.py -o forms.py
有些文件不需要在 git 中,所以讓我們為此設置配置:
echo -e "venv\n*.pyc\n*.log\n*.pot\nstaticfiles" > .gitignore
現在讓我們提交:
git init
git add .
git commit -m ‘initial commit of django app’
在 git 中創建一個存儲庫,然后復制 git url(以 .git 結尾的那個)。 然后:
git remote add origin THE_URL
git pull origin master
如果您不想為 github 付費並且希望您的存儲庫是私有的,則可以使用 bitbucket。
登錄到您的帳戶 創建一個新的存儲庫 單擊添加現有項目
git remote add origin https://USERNAME@bitbucket.org/USERNAME/REPOSITORY_NAME.git
即使您永遠不必擁有多個 heroku 帳戶,它也是一種設置和使用它的簡單方法,即使是一個帳戶也是如此。 所以我們繼續:
cd ~
heroku plugins:install git://github.com/ddollar/heroku-accounts.git
添加一個 heroku 帳戶:
heroku accounts:add personal
它在控制台中說,你必須這樣做:
將以下內容添加到您的 ~/.ssh/config
Host heroku.personal
HostName heroku.com
IdentityFile /PATH/TO/PRIVATE/KEY
IdentitiesOnly yes
使用以下內容轉到您的項目文件夾:
cd ~/PROJECT_WRAPPER
然后將新帳戶設置為:
heroku accounts:set personal
創建一個新的 ssh KEY:
ssh-keygen -t rsa
當要求輸入名稱時,請寫下完整路徑和名稱,如圖所示。 然后輸入您的密碼或留空
然后將密鑰添加到您的 OSX 和 heroku:
heroku keys:add ~/.ssh/YOUR_KEY_NAME.pub
ssh-add ~/.ssh/YOUR_KEY_NAME
現在您已經按順序擁有了密鑰,您應該可以執行 heroku 應用程序
並看到沒有應用程序。 添加您的第一個應用程序:
heroku apps:create YOUR_APP_NAME
現在上傳到服務器:
git push heroku master
現在去 YOUR_APP_NAME.herokuapp.com 看看你的網站!
如果有人想要,還有待解釋,讓我知道
深入文檔位於:
在我的另一個答案中,該過程得到了很好的描述,但需要時間。 所以我做了一個現成的安裝,可以在不到 15 分鍾的時間內完成。
https://github.com/mgpepe/django-heroku-15
如果您更喜歡長路徑的完整解釋,請參閱下面的答案。
這些是我在 DJANGO 上工作 2 年時發現的錯誤 [享受]
- 應該在 heroku->resources 中添加/看到 Dyno,如果沒有在 Heroku 的資源中添加它,則意味着在
“檔案”
web: gunicorn [django_project].wsgi --log-file -
上面的“django_project”是你的項目名,改成你的項目名
- 記得在settings.py文件中做修改
調試=真
ALLOWED_HOSTS = ["your-app.herokuapp.com","127.0.0.1"]
將此添加到
settings.py
文件中#->這應該在中間件中
'whitenoise.middleware.WhiteNoiseMiddleware',
#->this at the bottom
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
- [首先做“pip install django-heroku”]
把它放在
settings.py
文件的頂部:
import django_heroku
place this at the bottom of "settings.py" file:
django_heroku.settings(locals())
- Heroku 只適用於 postgres,記住這一點
[去https://www.elephantsql.com/]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "",
"USER": "",
"PASSWORD": "",
"HOST": "",
"PORT": "5432",
}
}
確保數據庫在后台,如果沒有運行,請在 services.msc 中啟動“postgres”。 [這是在任務管理器->服務->postgresql->右鍵單擊->開始]
python manage.py migrate
- 轉到heroku中的“您的應用程序”並轉到“設置”並在設置中選擇“添加buildpack”並選擇“python”
####################### 重要的 ########################## ####
==> 創建一個新的 Git 存儲庫 在新的或現有的目錄中初始化一個 git 存儲庫
cd my-project/
git init
heroku git:remote -a iris-django-ml
==> 部署您的應用程序
將您的代碼提交到存儲庫並使用 Git 將其部署到 Heroku。
git add .
git commit -am "make it better"
git push heroku master
- “在您的目錄中運行此命令”
heroku login
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
- 刪除工作目錄的 git 文件重新啟動,刪除 heroku 項目(設置 -> 底部)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.