Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update url.py from url to path #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions 06-first-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ from django.contrib import admin
from stores.views import home

urlpatterns = [
url(r'^$', home),
url(r'^admin/', include(admin.site.urls)),
path('', home),
path('admin/', admin.site.urls),
]
```

Expand Down Expand Up @@ -78,7 +78,7 @@ def home(request):
把 `lunch/urls.py` 中 `home` 的那行修改成下面這樣:

```python
url(r'^$', home, name='home'),
path('', home, name='home'),
```

2. 用這個 name 來 refer 我們要的 URL:
Expand Down
10 changes: 5 additions & 5 deletions 07-django-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ python manage.py dbshell

```
sqlite> .tables
auth_group django_admin_log
auth_group_permissions django_content_type
auth_permission django_migrations
auth_user django_session
auth_user_groups stores_menuitem
auth_group django_admin_log
auth_group_permissions django_content_type
auth_permission django_migrations
auth_user django_session
auth_user_groups stores_menuitem
auth_user_user_permissions stores_store
```

Expand Down
6 changes: 4 additions & 2 deletions 08-django-admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ INSTALLED_APPS = [

```python
urlpatterns = [
url(r'^$', home, name='home'),
path('', home, name='home'),

# 這一行把 admin/ 下面的 URL 對應到 Django admin。
url(r'^admin/', include(admin.site.urls)),
path('admin/', admin.site.urls),
]
```

Expand Down Expand Up @@ -108,6 +108,8 @@ class StoreAdmin(admin.ModelAdmin):

進入一個 `Store` 的 admin 頁面看看。那家店的菜單變成一個表格被列在下面,而且 Django 還多給你一個空白列,讓你可以繼續新增!方便多了吧。

![](assets/django-admin-store-inline-menu.png)

多多新增一些資料。之後會教你怎麼把這些東西列在網頁上給一般人看。

今天就到這裡!Django admin 還有很多很厲害的功能,有興趣的話請自行參照[文件](https://docs.djangoproject.com/en/dev/ref/contrib/admin/)。
20 changes: 10 additions & 10 deletions 09-model-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ from django.contrib import admin
from stores.views import home, store_list # 記得 import

urlpatterns = [
url(r'^$', home, name='home'),
url(r'^store/$', store_list, name='store_list'), # 新增這一行
url(r'^admin/', include(admin.site.urls)),
path('', home, name='home'),
path('store/', store_list, name='store_list'), # 新增這一行
path('admin/', admin.site.urls),
]
```

Expand Down Expand Up @@ -95,14 +95,14 @@ from django.contrib import admin
from stores.views import home, store_list, store_detail # 記得 import

urlpatterns = [
url(r'^$', home, name='home'),
url(r'^store/$', store_list, name='store_list'),
url(r'^store/(?P<pk>\d+)/$', store_detail, name='store_detail'), # 新增這行
url(r'^admin/', include(admin.site.urls)),
path('', home, name='home'),
path('store/', store_list, name='store_list'),
path('store/<int:pk>/', store_detail, name='store_detail'), # 新增這行
path('admin/', admin.site.urls),
]
```

`(?P<name>pattern)` 是 regular expression**named group**,就是根據 pattern 把抓到的 group 取名為 `name`,這在 `url` tag 裡面會用到,稍後解釋。`pk` 在這裡指 primary key,是 Django 慣用的命名法。
`<int:pk>` 是 Django 2.0url 變數,把抓到的 group 取名為 `name`,這在 `url` tag 裡面會用到,稍後解釋。`pk` 在這裡指 primary key,是 Django 慣用的命名法。

接者是 view function。在 URL 中被捕捉的值會直接被傳入,所以:

Expand Down Expand Up @@ -175,9 +175,9 @@ def store_detail(request, pk):

> 來比對一下 template 中的 `url` tag 與 `urls.py`的內容幫助理解順便複習概念:
> - `{% url 'store_detail' pk=store.pk %}`
> - `url(r'^store/(?P<pk>\d+)/$', store_detail, name='store_detail')`
> - `path('store/<int:pk>/', store_detail, name='store_detail')`
>
> url tag 的第一個 arg 會先去 urls.py 中找到 `name='store_detail' 的 url`,但由於我要拿到該 Store 的 url 還需要一個 pk 參數(不然誰知道你要拿哪家 store 的 url?),所以第二個 arg 我們就指定為該 `store`的`pk`、把`store.pk`傳入給`(?P<pk>\d+)`。
> url tag 的第一個 arg 會先去 urls.py 中找到 `name='store_detail' 的 url`,但由於我要拿到該 Store 的 url 還需要一個 pk 參數(不然誰知道你要拿哪家 store 的 url?),所以第二個 arg 我們就指定為該 `store`的`pk`、把`store.pk`傳入給`<int:pk>`。

如果你熟悉 CRUD 的話,我們今天實作的其實就是那個 R。其實不難吧!不過上面的程式其實不是很優秀,所以我們明天會花一點來改寫,讓它符合 best practice。

Expand Down
Binary file added assets/django-admin-store-inline-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.