Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
## 學習前準備
在使用這份指南前,請先準備好:

1. [安裝 Python 3.5](http://djangogirlstaipei.herokuapp.com/tutorials/installation/)
1. [安裝 Python 3.4 以上](http://djangogirlstaipei.herokuapp.com/tutorials/installation/)
2. [註冊 PythonAnywhere](https://www.pythonanywhere.com/)


Expand Down
17 changes: 8 additions & 9 deletions django/admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

大部份網站都設計有管理後台,讓管理者方便新增或異動網站內容。

而這樣的管理後台,Django 也有內建一個 App -- [**Django Admin**](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/) 。只需要稍微設定,網站就能擁有管理後台功能。
而這樣的管理後台,Django 也有內建一個 App -- [**Django Admin**](https://docs.djangoproject.com/en/2.0/ref/contrib/admin/) 。只需要稍微設定,網站就能擁有管理後台功能。

前一章,我們學到如何使用 Django Model 抽象地表達資料庫結構。現在,我們要透過 **Django Admin** 看到實際的資料,並跟資料庫進行互動。

Expand All @@ -21,10 +21,10 @@
```
# mysite/settings.py

INSTALLED_APPS = (
INSTALLED_APPS = [
'django.contrib.admin',
...
)
]
```

當你在同步資料庫時,也會建立需要的資料表及欄位。
Expand All @@ -36,14 +36,14 @@ INSTALLED_APPS = (
我們將管理後台的網址設定為 `/admin/`。確認 `mysite/urls.py` 中的 `urlpatterns` 包含下面這行:

```
url(r'^admin/', include(admin.site.urls)),
path('admin/', admin.site.urls),
```

## 建立 superuser

要使用 Django 的管理後台,需要一個管理員帳號。

使用 [createsuperuser](https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-createsuperuser) 這個指令,建立一個 superuser:
使用 [createsuperuser](https://docs.djangoproject.com/en/2.0/ref/django-admin/#django-admin-createsuperuser) 這個指令,建立一個 superuser:

```
(djangogirls_venv) ~/djangogirls/mysite$ python manage.py createsuperuser
Expand All @@ -55,6 +55,7 @@ Superuser created successfully.

```
輸入帳號、Email、密碼等資訊,就完成 superuser 的新增了。
注意:密碼不能太常見,並且需要超過八個字元。


## 註冊 Model class
Expand All @@ -65,13 +66,11 @@ Superuser created successfully.

```python
# trips/admin.py

from django.contrib import admin
from .models import Post

from .models import Post

admin.site.register(Post)

```

## 使用管理後台
Expand Down Expand Up @@ -101,7 +100,7 @@ admin.site.register(Post)

---

Django 通常以 `Post object` 來表示 Post 物件,但此種顯示不易辨別。我們可以透過 [`def __str__`](https://docs.djangoproject.com/en/1.8/ref/models/instances/#str) 更改 Post 的表示方式。
Django 預設以 `Post object` 來表示 Post 物件,但此種顯示不易辨別。我們可以透過 [`def __str__`](https://docs.djangoproject.com/en/2.0/ref/models/instances/#str) 更改 Post 的表示方式。

修改 `trips/models.py`:

Expand Down
27 changes: 11 additions & 16 deletions django/dynamic_url.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@

```python
# trips/views.py

# ...

from django.shortcuts import render

from .models import Post

# ...

def post_detail(request, pk):
post = Post.objects.get(pk=pk)
Expand Down Expand Up @@ -52,33 +51,29 @@ from trips.views import hello_world, home, post_detail

urlpatterns = [
...
url(r'^post/(?P<pk>\d+)/$', post_detail, name='post_detail'),
path('post/<int:pk>/', post_detail, name='post_detail'),
]
```

上面的修改完成後,只要連至`http://127.0.0.1/post/3/` 就會對應到 `post_detail()` 這個 view,並且**傳入的 pk=3** 。

---

### 使用 Regex 提取部份 URL 為參數
### 使用 Route 字串將參數從 URL 中提取出來

我們前面提過,Django 的 URL 是一個 *regular expression (regex)*。Regular expression 語法可用來描述一個字串的樣式。 除了可以表示固定字串之外,還可以用來表示不確定的內容。我們一步一步解釋文章單頁所使用的 URL 設定:
前面提過,如何使用 route 定義 URL 規則,讓 Django 知道該對應到哪個 view function。 除了可以表示固定字串之外,還可以用來表示動態的內容。我們一步一步解釋文章單頁所使用的 URL 設定:

```
(?P<pk>\d+)
'post/<int:pk>/'
```

1. `\d` 代表一個阿拉伯數字。

2. `+` 代表「一個以上」。

所以 `\d+` 代表一個以上的阿拉伯數字,例如「0」、「99」、「12345」。可是像「8a」就不符合,因為「a」不是數字。
1. `post/` 表示 URL 前面為 **post/** 開頭。

3. `(?P<pk>)` 代表「把這一串東西抓出來,命名為 pk
2. `int` 定義參數必須為整數

所以 `(?P<pk>\d+)` 代表:抓出一個以上阿拉伯數字,並把抓出來的東西取名為 pk。
3. `<int:pk>` 將傳入的整數,命名為 pk。

綜合以上的規則,`r'^post/(?P<pk>\d+)/$'` 會達成以下的效果:
綜合以上的規則,`'post/<int:pk>/'` 會達成以下的效果:

URL | 符合結果
----------|------------------------
Expand Down Expand Up @@ -160,7 +155,7 @@ return render(request, 'post.html', {'post': post})
{% url '<url_name>' arg1=<var1> arg2=<var2> ...%}
```

其餘用法可參考[官方文件](https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url)。
其餘用法可參考[官方文件](https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#url)。

---

Expand Down
37 changes: 13 additions & 24 deletions django/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

---

首先,請開啟**終端機**,確定現在的位置是在**家目錄**底下
首先,請開啟**終端機**,確定現在的位置是在**家目錄**底下

我們先建立一個名為 `djangogirls` 的資料夾
我們先建立一個名為 `djangogirls` 的資料夾

```
mkdir djangogirls
```

並切換至剛剛建立的目錄
並切換至剛剛建立的目錄

```
cd djangogirls
Expand Down Expand Up @@ -82,44 +82,33 @@ Linux 或 OS X 需要使用 `python3` 來建立虛擬環境,指令如下:
(djangogirls_venv) ~/djangogirls$


## 安裝 Django 1.8 最新版本
## 安裝 Django 2.0 最新版本

### 開始安裝

Python 3.4 預先安裝了 `pip` 這個強大的套件管理工具,我們將使用它來安裝 Django:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這裡的 3.4 很多餘

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這個之前就有了啦,pip 真的是 3.4 之後才有的啊

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可是現在還在說 3.4 不覺得很多餘嗎,就說「Python 現在預先安裝了」就好

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因為無法確定看 Gitbook 的人 Python 的版本,想說這邊至少說清楚 3.4 才有 pip。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等等 Django 2.0 根本就不支援 3.4 以前的版本啊,如果是用沒有 pip 的 Python 從教材第一頁就應該要出局了?


```
(djangogirls_venv) ~/djangogirls$ pip install "django<1.9"
(djangogirls_venv) ~/djangogirls$ pip install django~=2.0
```

這裡需要特別注意,我們使用的指令是 `"django`**`<1.9`**`"`。這樣一來才可以**確保我們安裝的是 Django 1.8 的最新版本**

輸入了應該會看到如下的訊息,表示安裝成功
輸入以後應該會看到如下的訊息,表示安裝成功。

```
Installing collected packages: django
Successfully installed django-1.8.6
Installing collected packages: pytz, django
Successfully installed django-2.0.5 pytz-2018.4
```

註:如果你看到以 *Fatal error in launcher* 開頭的輸出,而不是上面的安裝成功訊息,請改用 `python -m pip install "django<1.9"` 試試看。之後如果在使用 `pip` 時遇到類似問題,也可以試著在前面加上 `python -m`。
註:如果你看到以 *Fatal error in launcher* 開頭的輸出,而不是上面的安裝成功訊息,請改用 `python -m pip install django` 試試看。之後如果在使用 `pip` 時遇到類似問題,也可以試著在前面加上 `python -m`。


### 確認安裝成功

最後,讓我們最後來測試一下。

請在虛擬環境下指令輸入 `python`,進入**互動式命令列**環境

```
(djangogirls_venv) ~/djangogirls$ python
```

輸入以下的指令取得 Django 版本資訊:
最後,讓我們來確定一下安裝的版本是否正確,請在虛擬環境下輸入:

```
>>> import django
>>> django.VERSION
(1, 8, 6, 'final, 0')
(djangogirls_venv) ~/djangogirls$ python -m django --version
2.0.5
```

如果看見類似上面的訊息,就代表安裝成功囉!
出現的數字代表虛擬環境中 Django 的版本資訊,如果看見類似上面的訊息,就代表安裝成功囉!
26 changes: 12 additions & 14 deletions django/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ DATABASES = {
- **ENGINE** -- 你要使用的資料庫引擎。例如:
- MySQL: `django.db.backends.mysql`
- SQLite 3: `django.db.backends.sqlite3`
- PostgreSQL: `django.db.backends.postgresql_psycopg2`
- PostgreSQL: `django.db.backends.postgresql`
- **NAME** -- 你的資料庫名稱

如果你使用 MySQL 或 PostgreSQL 等等資料庫,可能還要設定它的位置、名稱、使用者等等。不過我們這裡使用的 SQLite 3 不需要這些性質,所以可以省略。
Expand Down Expand Up @@ -75,29 +75,29 @@ class Post(models.Model):

---

[**Model fields**](https://docs.djangoproject.com/en/1.8/ref/models/fields/) 可為 Django Model 定義不同型態的屬性。
[**Model Field**](https://docs.djangoproject.com/en/2.0/ref/models/fields/) 可為 Django Model 定義不同型態的屬性。

- **CharField** -- **字串欄位**,適合像 title、location 這種有長度限制的字串。
- **TextField** -- **合放大量文字的欄位**
- **URLField** -- **URL 設計的欄位**
- **DateTimeField** -- **日期與時間的欄位**,使用時會轉成 Python `datetime` 型別。

更多 Model Field 與其參數,請參考 [Django 文件 ](https://docs.djangoproject.com/en/1.8/ref/models/fields/)
更多 Model Field 與其參數,請參考 [Django 文件](https://docs.djangoproject.com/en/2.0/ref/models/fields/)

---

## 同步資料庫

首先執行 [`makemigrations`](https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-makemigrations) 指令:
首先執行 [`makemigrations`](https://docs.djangoproject.com/en/2.0/ref/django-admin/#django-admin-makemigrations) 指令:

```
(djangogirls_venv) ~/djangogirls/mysite$ python manage.py makemigrations
Migrations for 'trips':
0001_initial.py:
trips/migrations/0001_initial.py
- Create model Post
```

這個指令會根據你對 Model 的修改刪除建立一個新的 [migration 檔案](https://docs.djangoproject.com/en/1.8/topics/migrations/#migration-files),讓 `migrate` 指令執行時,可以照著這份紀錄更新資料庫。
這個指令會根據你對 Model 的修改刪除建立一個新的 [migration 檔案](https://docs.djangoproject.com/en/2.0/topics/migrations/#migration-files),讓 `migrate` 指令執行時,可以照著這份紀錄更新資料庫。

接著用以下的指令,讓 Django 根據上面的紀錄,把 `models.py` 中的欄位寫入資料庫:

Expand All @@ -109,25 +109,23 @@ Migrations for 'trips':

```
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: sessions, admin, auth, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Apply all migrations: admin, auth, contenttypes, sessions, trips
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
Applying trips.0001_initial... OK
```

[`migrate`](https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-migrate) 指令會根據 `INSTALLED_APPS` 的設定,按照 app 順序建立或更新資料表,將你在 models.py 裡的更新跟資料庫同步。
[`migrate`](https://docs.djangoproject.com/en/2.0/ref/django-admin/#django-admin-migrate) 指令會根據 `INSTALLED_APPS` 的設定,按照 app 順序建立或更新資料表,將你在 models.py 裡的更新跟資料庫同步。
13 changes: 6 additions & 7 deletions django/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@

- 修改 HTML 與 CSS,調整成你喜歡的樣子
- 為旅遊日記添加新的欄位(例如旅遊日期),並使用 `makemigrations` 和 `migrate` 更新資料庫。
- 為旅遊日記加入作者(提示:你可能會需要修改 Model,並與 [Django 使用者認證](https://docs.djangoproject.com/en/1.7/topics/auth/) 功能整合)
- 將 HTML 重複的部分獨立出來共用(提示:使用 [Template 繼承](https://docs.djangoproject.com/en/1.7/topics/templates/#template-inheritance))
- 為每一篇日記加上留言板(提示:[ Django Forms](https://docs.djangoproject.com/en/dev/topics/forms/) 可以幫助你更快速地完成)
- 為旅遊日記加入作者(提示:你可能會需要修改 Model,並與 [Django 使用者認證](https://docs.djangoproject.com/en/2.0/topics/auth/) 功能整合)
- 將 HTML 重複的部分獨立出來共用(提示:使用 [Template 繼承](https://docs.djangoproject.com/en/2.0/topics/templates/#template-inheritance))
- 為每一篇日記加上留言板(提示:[ Django Forms](https://docs.djangoproject.com/en/2.0/topics/forms/) 可以幫助你更快速地完成)


## 其他學習資源

- [Codecademy](http://www.codecademy.com/learn) -- 透過闖關遊戲方式學習 Python, HTML/CSS, JavaScript
- [Writing your first Django app](https://docs.djangoproject.com/en/1.8/intro/tutorial01/) -- Django 1.8 官方學習指南
- [Getting Started With Django](http://gettingstartedwithdjango.com/) -- 影片課程
- [The Django Book](https://django-book.readthedocs.org/en/latest/) -- 雖然 Django 版本不是最新,但相當適合初學者的一本書
- [Two Scoops of Django: Best Practices for Django](http://twoscoopspress.org/products/two-scoops-of-django-1-8) -- 非常推薦,曾經在 [Taipei.py](http://www.meetup.com/Taipei-py/) 隔週二聚會指定書籍
- [Writing your first Django app](https://docs.djangoproject.com/en/2.0/intro/tutorial01/) -- Django 2.0 官方學習指南
- [The Django Book](https://djangobook.com/) -- 非常適合初學者的一本書,目前最新版本為 Django 1.11 LTS
- [Two Scoops of Django: Best Practices for Django](https://www.twoscoopspress.com/products/two-scoops-of-django-1-11) -- Django 的最佳實踐範例(Django 1.11 LTS),曾經為 [Taipei.py](http://www.meetup.com/Taipei-py/) 隔週二聚會指定書籍
- [Django Packages](https://www.djangopackages.com/) -- Django 相關套件彙整平台,提供搜尋和評比

---
Expand Down
16 changes: 8 additions & 8 deletions django/orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
我們一樣可以用 pip 來安裝這個強大的套件:

```
(djangogirls_venv) ~/djangogirls/mysite$ pip install ipython[terminal]
(djangogirls_venv) ~/djangogirls/mysite$ pip install ipython
```

安裝完畢後,就可以使用 [shell](https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-shell) 指令,進入 Django Shell:
安裝完畢後,就可以使用 [shell](https://docs.djangoproject.com/en/2.0/ref/django-admin/#django-admin-shell) 指令,進入 Django Shell:

```
(djangogirls_venv) ~/djangogirls/mysite$ python manage.py shell
Expand Down Expand Up @@ -61,7 +61,7 @@
### Read

若想顯示所有的 Post ,可以使用
[all()](https://docs.djangoproject.com/en/1.8/ref/models/querysets/#django.db.models.query.QuerySet.all):
[all()](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#django.db.models.query.QuerySet.all):

```
>>> from trips.models import Post
Expand All @@ -79,16 +79,16 @@
[<Post: My First Trip>, <Post: Django 大冒險>]
```

- [**get**](https://docs.djangoproject.com/en/1.8/ref/models/querysets/#get):返回符合條件的**唯一一筆資料**。(*注意:*如果找不到符合條件的資料、或是有多筆資料符合條件,都會產生 exception)
- [**get**](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#get):返回符合條件的**唯一一筆資料**。(*注意:*如果找不到符合條件的資料、或是有多筆資料符合條件,都會產生 exception)

- [**filter**](https://docs.djangoproject.com/en/1.8/ref/models/querysets/#filter):返回符合條件的陣列。如果找不到任何資料則會返回空陣列。
- [**filter**](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#filter):返回符合條件的陣列。如果找不到任何資料則會返回空陣列。


### Update

當想修改資料時,可以使用 [update](https://docs.djangoproject.com/en/1.8/ref/models/querysets/#django.db.models.query.QuerySet.update) 更新一筆或多筆資料:
當想修改資料時,可以使用 [update](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#django.db.models.query.QuerySet.update) 更新一筆或多筆資料:

首先,這裡使用 [contains](https://docs.djangoproject.com/en/1.8/ref/models/querysets/#contains) 針對`title`欄位,篩選出所有標題中包含 `Trip` 字眼的 Post
首先,這裡使用 [contains](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#contains) 針對`title`欄位,篩選出所有標題中包含 `Trip` 字眼的 Post

```
>>> posts = Post.objects.filter(title__contains='Trip')
Expand Down Expand Up @@ -138,7 +138,7 @@

### Delete

我們也可以使用 [delete](https://docs.djangoproject.com/en/1.8/ref/models/querysets/#django.db.models.query.QuerySet.delete) 刪除資料:
我們也可以使用 [delete](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#django.db.models.query.QuerySet.delete) 刪除資料:

我們試著使用 `delete`,將剛剛的那兩筆 Post 刪除。

Expand Down
Loading