建立新專案
建立 Project
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 新的project內容如下
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
mysite 根目錄,可以改名
manage.py 與專案互動的命令程式
__init__.py 一個空的檔案,告訴 Python 要把這個資料夾視為 Python 套件之一 (不用理他)
settings.py 設定檔,說明它有哪些 APP 、 database等等
urls.py 設定網址(例如: /about, /blogs, /contact 等等
asgi.py 提供一個進入點,讓 ASGI-compatible web servers 可以連到 project,專案放到網路上可以持續運行
wsgi.py 提供一個進入點,讓 WSGI-compatible web servers 可以連到 project
以下步驟都要在(各自的)虛擬環境下執行,多個專案之間才不會互相干擾
運行 server
- runserver
- runserver 默認的 server 是內部 IP Port 8000。但若電腦有裝 xampp 等伺服器軟體,可能會占用最常用的 Port(ex. :8000, :8020),指定Port開另外一個通道(ex. 8080)
點網址,看到火箭代表成功
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
建立 app
app 也會自動建立好
- 新的 app 內容如下
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
views.py存放 view
models.py存放model
tests.py
admin.py
apps.py登記APPs
migrations 存"migrations"(讓你可以依照 model 寫的方法去自動更新資料庫)
db.sqlite3 資料庫檔案(簡易 sqlite database)
- mappings, templates, and static
- settings詳細內容 https://docs.djangoproject.com/en/3.2/ref/settings/#databases
資料遷移(建立資料表)
- 資料庫建立的過程
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
寫 app
註冊APP
- app 建立後,要讓 Django project 知道
- 在
(project名)/settings.py 中設定,找到其中的 'INSTALLED_APPS',在最後加入 app名
- 參考Django settings, Settings
寫view
- app 的靈魂,寫這個 app 要做什麼事
- 最簡單也最重要的功能: request & response
連結 urls (app內)
- 很重要
- 參考URL dispatcher
- 建新檔案(
urls.py ),紀錄連結 url 到 view
- 種類
- Function views
- Add an import: from my_app import views
- Add a URL to urlpatterns: path('', views.home, name='home')
- Class-based views
- Add an import: from other_app.views import Home
- Add a URL to urlpatterns: path('', Home.as_view(), name='home')
- Including another URLconf
- Import the include() function: from django.urls import include, path
- Add a URL to urlpatterns: path('blog/', include('blog.urls'))
連結 urls (root 和 app)
資料遷移(同步資料表)
- 告訴 Django 模型有所變動,這會建立一個遷移(migration)檔案,紀錄了資料庫的變更
- 每當資料庫有變動就要同步
- 沒有資料變動的情況

運行 server