close

使用 pythonanywhere與Github 部署 Django 網站

1.將Django專案上傳至github

使用 freeze 創建 requirements.txt 目錄

使用終端機在專案目錄創建git倉庫

git init

將本地檔案加入追蹤

git add . 

添加描述

git commit -m '描述'

連接 github 並進行使用者登入

git remote add origin https://github.com/<github_username>/<project_name.git>

上傳檔案

git push -u origin master .

2.申請pythonanywhere帳戶

https://www.pythonanywhere.com

3.上傳github專案至pythonanywhere

在 pythonanywhere 點選 Start a new console

選擇 Bash 開啟終端機輸入以下指令

git clone https://github.com/<github_username>/<project_name.git>

4.在pythonanywhere Bash 進行專案配置

在Bash中繼續進行以下操作

切換進入專案目錄

cd <project_name>

創建虛擬環境並且啟用

virtualenv --python=python3.6 myvenv
source myvenv/bin/activate

安裝所需要的套件

pip3 install -r requirements.txt

創建資料庫與超級使用者

python manage.py makemigrations
python manage.py createsuperuser 

5.pythonanywhere Web 配置

切換 Web 頁面,並且選擇  add a new app

選擇 manual configuration (並非python3.6)

設置虛擬環境位置

在 Virtualenv下添加以下路徑

/home/<github_username>/<project_name>/myvenv

在 Static files 下添加以下路徑

URL:

/static/  

Directory:

/home/<pythonanywhere_username>/<project_name>/static/

開啟 WSGI configuration file 進行配置:

將django部份的註解取消,並修改以下部份

path = '/home/<pythonanywhere_username>/<project_name>'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = '<project_name>.settings' #存放 settings.py的目錄名稱

6.測試網站

點選Web頁面的 Reload 後開啟網站

成功開啟的話就代表大功告成了

如果失敗的話可以點選 error.log 察看錯誤原因

arrow
arrow
    全站熱搜

    ivankao 發表在 痞客邦 留言(0) 人氣()