diff --git a/first_django_app.md b/first_django_app.md index 7a78c64..6b60364 100644 --- a/first_django_app.md +++ b/first_django_app.md @@ -172,4 +172,15 @@ Here `path()` function is passed **route** and **view**; two additional option a ### Test API -进入 `python manage.py shell` 可以使用Django创建的各种API,如[数据库抽象API database API(建议细看)](https://docs.djangoproject.com/zh-hans/3.1/topics/db/queries/) \ No newline at end of file +进入 `python manage.py shell` 可以使用Django创建的各种API,如[数据库抽象API database API(建议细看)](https://docs.djangoproject.com/zh-hans/3.1/topics/db/queries/) + +## Part 3 + +* In Django, web content & HTML come from Views models. Every view is a Python function/class. Django use customer requested URL to decide which View to generate. +* Django use 'URLconfs' to map btw URL and Views. Details of[pURL manager](https://docs.djangoproject.com/zh-hans/3.1/topics/http/urls/) +* URL's general form: `/newsarchive///` + +### Create more Views + +> 当某人请求你网站的某一页面时——比如说, "/polls/34/" ,Django 将会载入 mysite.urls 模块,因为这在配置项 ROOT_URLCONF 中设置了。然后 Django 寻找名为 urlpatterns 变量并且按序匹配正则表达式。在找到匹配项 'polls/',它切掉了匹配的文本("polls/"),将剩余文本——"34/",发送至 'polls.urls' URLconf 做进一步处理。在这里剩余文本匹配了 "/",使得我们 Django 以如下形式调用 polls.urls.detail(): +> `detail(request=, question_id=34)` diff --git a/src/polls/urls.py b/src/polls/urls.py index 3ef24d9..7c66085 100644 --- a/src/polls/urls.py +++ b/src/polls/urls.py @@ -3,5 +3,12 @@ from django.urls import path from . import views urlpatterns = [ + # ex: /polls/ path('', views.index, name='index'), + # ex: /polls/5/ + path('/', views.detail, name='detail'), + # ex: /polls/5/results/ + path('/results/', views.results, name='results'), + # ex: /polls/5/vote/ + path('/vote/', views.vote, name='vote'), ] \ No newline at end of file diff --git a/src/polls/views.py b/src/polls/views.py index ad2c866..c76691c 100644 --- a/src/polls/views.py +++ b/src/polls/views.py @@ -2,4 +2,14 @@ from django.http import request, HttpResponse from django.shortcuts import render def index(request): - return HttpResponse("Hello, world. You're at the polls index") \ No newline at end of file + return HttpResponse("Hello, world. You're at the polls index") + +def detail(request, question_id): + return HttpResponse("You're looking at question %s." % question_id) + +def results(request, question_id): + response = "You're looking at the results of question %s." + return HttpResponse(response % question_id) + +def vote(request, question_id): + return HttpResponse("You're voting on question %s." % question_id) \ No newline at end of file