Skip to content

DysonMa/Madi-Ecommerce

Repository files navigation

Madi-Ecommerce

  • 這是一個用 Django, Bootstrap, MySQL(或SQLite), Docker 架設並部署在 Heroku 的購物網站。

home

功能

  • 響應式(RWD)網站設計
  • 首頁圖片輪播(Carousel)
  • 首頁商品導覽換頁(Paginator)
  • 使用者登入驗證與註冊(django auth)
  • 第三方登入驗證(Google、GitHub)
  • 利用URL patterns增刪購物車內容(Restful API)
  • 將商品加入購物車(django-shopping-cart)並提交訂單(session)
  • 發送訂單確認的電子郵件給顧客(SMTP + Gmail)
  • Admin管理後臺可以上傳產品圖片(django-filer)

使用技術與工具

演示步驟與展示圖片

  • 尚未有帳號密碼,所以導至登入頁面

  • 第三方登入(Google、GitHub)

  • 或是註冊帳號,成功後會寄發註冊成功的電子郵件

  • 登入成功後,進入首頁

  • 使用者個人資料

  • 分類檢視,例如選擇"書籍"

  • 選購商品,加入購物車

  • 按下"我要訂購",並輸入訂購資訊,再按下"下訂單"

  • 發送電子郵件通知訂單給顧客

  • 若想增加商品圖片,可以在Admin管理後臺上傳圖片

資料表設計與正規化

底下為MySQL Workbench的EER Diagram

  • myshop_product: 是 商品 的資料表,有 sku, name, description, stock, price...等欄位

  • myshop_category: 是 商品類別 的資料表,有 name 這個欄位,並以 category_id 為 Foreign key 與 myshop_product 資料表做關聯

  • auth_user: 是 Django auth 內建的 user(使用者) model,有內建的欄位,ex: password, last_login, username...

  • myshop_profile: 擴充內建的 user(使用者) model,多增加一個 male的欄位,並以 user_id 為 foreign key 與 auth_user 資料表做關聯

  • myshop_order: 按下 "我要訂購" 所儲存的資料表,內含訂購填寫的 full_name, address, phone...等欄位,並以 user_id 為 foreign key 與 auth_user 資料表做關聯

  • myshop_orderitem: 按下 "下訂單" 所儲存的資料表,內含訂單詳細資訊如 price, quantity, name...等欄位,並以 order_id 為 foreign key 與 myshop_order 資料表做關聯

  • cart_item: 是 Django-shopping-cart 內建的購物車內容 的資料表,有 quantity, unit_price...等欄位

本地端建立環境

安裝

遠端下載repo

git clone https://github.com/DysonMa/Madi-Ecommerce.git

編輯config.ini

以下是範本,要依照個人需求修改

[Django]
SECRET_KEY = 

[Gmail]
email = 
client_secret = 

[MySQL]
client_secret = 
  • [Django] SECRET_KEY: 可以不用編輯,因為是demo版,所以仍然附在code裡面
  • [Gmail] email: 輸入寄件者的信箱
  • [Gmail] client_secret: 用來核准寄信的密鑰,須至 Google Gmail API 申請 申請,詳細步驟可以參考這裡
  • [MySQL] client_secret: 用來連線MySQL的密碼

手動啟動

  1. 進入資料夾
cd Madi-Ecommerce
  1. 創建一個虛擬環境叫做 myenv
python -m venv myvenv
  1. 開啟虛擬環境
myvenv\Scripts\activate
  1. pip安裝相依的套件
pip install -r requirements.txt
  1. migrate資料
python manage.py makemigrations
python manage.py migrate
  1. 建立superuser,輸入使用者名稱/Password
python manage.py createsuperuser
  1. 在本地端 run server
python manage.py runserver
  1. https://localhost:8000/ 就可以看到網站
  2. 利用剛剛輸入的使用者名稱/Password登入網站

利用Docker啟動

  1. 進入資料夾
cd Madi-Ecommerce
  1. 利用 docker-compose 建立並啟動環境
docker-compose up --build
docker-compose up
  1. 查看 container 的 ID
docker ps -a
  1. 進入bash
docker exec -it <Web Container ID> bash
  1. 在 bash 輸入指令,migrate資料
python manage.py makemigrations
python manage.py migrate
  1. 在 bash 輸入指令,建立superuser,輸入使用者名稱/Password
python manage.py createsuperuser
  1. https://localhost:8000/ 就可以看到網站
  2. 利用剛剛輸入的使用者名稱/Password登入網站

手動增加商品

  1. 因為資料庫是全新的,所以商品需要手動加入
  2. 點選上方導覽列的 後臺管理,再點選左邊 MYSHOP/Categorys,點選右上方的 新增 CATEGORY,輸入想增加的商品類別
  3. 點選左邊 MYSHOP/Products,點選右上方的 新增 PRODUCT,會看到下圖即可開始依照欄位建立商品,圖片可以從 static/images裏頭上傳

第三方登入驗證申請步驟

  1. 需要到 Google-API-Dashboard 申請一組憑證,並記錄下來
  2. 需要到 Github 第三方驗證上方申請一組憑證,並記錄下來
  3. 點選上方導覽列的 後臺管理,再點選左邊 網站/網站,點選中間的 example.com,網域名稱改為 localhost:8000/,顯示名稱改為 localhost
  4. 再點選左邊 社群帳號/社群應用程式,點選右上方的 新增 社群應用程式,進到下面畫面,選擇Google或GitHub,並輸入剛剛申請的憑證,接著別忘了底下的 localhost:8000/ 要加入到右邊,底下是Google的範例。

詳細教學:

Heroku部署步驟

專案上遇到的問題

Config組態設定

  • 因為部署到 github 會有 client_secret 外露的資安問題,所以編輯一個 config.ini 的組態設定檔,內含各種密碼,透過 configer 的讀取方式,避免暴露密碼,也方便統一管理所需要的參數

Django套件

  • django-shopping-cart 內建的 add 函式無法存取 product,導致 OrderItem 這個 model 無法跟 Product 這個 model 以 foreign key 做關聯,地端程式碼可以手動修改,但部署到 Heroku 就無法修改套件py檔內容,因此最後 Heroku app 取消關聯到 Product 這個資料表
  • python manage.py makemigrations 若失效,要進到 migrations 資料夾,手動查找每個版本的py檔,找出與model修正有關聯的檔案,手動修正或刪除,重新執行此指令即可成功運行

Heroku部署

  • 本機作業環境是 Windows,利用 pip freeze > requirements.txt 指令會生成 pywin300這個套件,但Heroku是Linux環境,所以會部署失敗,需要手動刪除此套件
  • Heroku 預設的資料庫是 PostgreSQL,因此 settings 裏頭要修改DB連結的方式,才能在 Heroku 上部署成功
  • Heroku 部署需要提供三個檔案,Procfilerequirements.txtruntime.txt

Backend學習筆記與部落格

About

Utilize Django framework to build Ecommerce platform

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors