建立新專案

tags: Django

建立 Project

django-admin startproject mysite

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

以下步驟都要在(各自的)虛擬環境下執行,多個專案之間才不會互相干擾

pipenv shell

運行 server

  • runserver
    • runserver 默認的 server 是內部 IP Port 8000。但若電腦有裝 xampp 等伺服器軟體,可能會占用最常用的 Port(ex. :8000, :8020),指定Port開另外一個通道(ex. 8080)
py manage.py runserver 或 py manage.py runserver 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

py manage.py startapp polls

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

資料遷移(建立資料表)

py manage.py migrate
  • 資料庫建立的過程
    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

  • app 建立後,要讓 Django project 知道
  • (project名)/settings.py 中設定,找到其中的 'INSTALLED_APPS',在最後加入 app名
  • 參考Django settings, Settings
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls', #這裡 ]

寫view

  • app 的靈魂,寫這個 app 要做什麼事
  • 最簡單也最重要的功能: request & response
from django.shortcuts import render from django.http import HttpResponse # Create your views here. def index(request): return HttpResponse("Hello, world. You're at the polls index.")

連結 urls (app內)

  • 很重要
  • 參考URL dispatcher
  • 建新檔案( urls.py ),紀錄連結 url 到 view
  • 種類
    • Function views
      1. Add an import: from my_app import views
      2. Add a URL to urlpatterns: path('', views.home, name='home')
    • Class-based views
      1. Add an import: from other_app.views import Home
      2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
    • Including another URLconf
      1. Import the include() function: from django.urls import include, path
      2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
from django.contrib import admin from django.urls import path from . import views #'.': 本地資料夾 urlpatterns = [ # 路徑:views中的index path('', views.index, name='index'),

連結 urls (root 和 app)

  • 將 app 的 urls 表指向根目錄
  • 參考includes()
from django.contrib import admin from django.urls import include, path #加include urlpatterns = [ path('polls/', include('polls.urls')), #引入poll.urls path('admin/', admin.site.urls), #首頁 ]

資料遷移(同步資料表)

  • 告訴 Django 模型有所變動,這會建立一個遷移(migration)檔案,紀錄了資料庫的變更
  • 每當資料庫有變動就要同步
py manage.py makemigrations polls(APP名稱)
  • 沒有資料變動的情況

運行 server