# 建立新專案
###### tags: `Django`
## 建立 Project
- 參考[Writing your first Django app, part 1](https://docs.djangoproject.com/en/3.2/intro/tutorial01/)
- 用 admin 建立新 project
```shell=
django-admin startproject mysite
```

- 新的project內容如下

- `mysite` 根目錄,可以改名
- `manage.py` 與專案互動的命令程式
- `__init__.py` 一個空的檔案,告訴 Python 要把這個資料夾視為 Python 套件之一 (不用理他)
- `settings.py` 設定檔,說明它有哪些 APP 、 database等等
- `urls.py` 設定網址(例如: /about, /blogs, /contact 等等
- [URL是什麼?它與 Domain 網域的差異在哪?](https://3csilo.com/what-is-url/)
- `asgi.py` 提供一個進入點,讓 ASGI-compatible web servers 可以連到 project,專案放到網路上可以持續運行
- `wsgi.py` 提供一個進入點,讓 WSGI-compatible web servers 可以連到 project
:::warning
以下步驟都要在(各自的)虛擬環境下執行,多個專案之間才不會互相干擾
```shell=
pipenv shell
```
:::
## 運行 server
- runserver
- runserver 默認的 server 是內部 IP Port 8000。但若電腦有裝 xampp 等伺服器軟體,可能會占用最常用的 Port(ex. :8000, :8020),指定Port開另外一個通道(ex. 8080)
```shell=
py manage.py runserver
或
py manage.py runserver 8080(指定通道)
```
點網址,看到火箭代表成功


## 建立 app
```shell=
py manage.py startapp polls
```
app 也會自動建立好
- 新的 app 內容如下

- `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
## 資料遷移(建立資料表)
- 參考[建立 App 與模型](https://openhome.cc/Gossip/CodeData/PythonTutorial/AppModelPy3.html), [Migrations](https://docs.djangoproject.com/en/3.2/topics/migrations/)
- 在資料庫中建立相應的資料表及欄位
```shell=
py manage.py migrate
```
- 資料庫建立的過程

## 寫 app
- 一個功能就是一個 app
### 註冊APP
- app 建立後,要讓 Django project 知道
- 在 `(project名)/settings.py` 中設定,找到其中的 'INSTALLED_APPS',在最後加入 `app名`
- 參考[Django settings](https://docs.djangoproject.com/en/3.2/topics/settings/), [Settings](https://docs.djangoproject.com/en/3.2/ref/settings/)
```python=
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
```python=
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](https://docs.djangoproject.com/en/3.2/topics/http/urls/)
- 建新檔案( `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'))
```python=
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()](https://docs.djangoproject.com/en/3.2/ref/urls/#django.urls.include)
```python=
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)檔案,紀錄了資料庫的變更
- **每當資料庫有變動就要同步**
```shell=
py manage.py makemigrations polls(APP名稱)
```
- 沒有資料變動的情況

## 運行 server
- app 建完再次運行會跟一開始不一樣了

- http://127.0.0.1:8000/ -> 根目錄(admin)
- http://127.0.0.1:8000/polls/ -> 頁面(app)
