3.1 Create more Views
parent
164cc17ff9
commit
8de2a0a82a
|
@ -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/)
|
||||
进入 `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/<year>/<month>/`
|
||||
|
||||
### Create more Views
|
||||
|
||||
> 当某人请求你网站的某一页面时——比如说, "/polls/34/" ,Django 将会载入 mysite.urls 模块,因为这在配置项 ROOT_URLCONF 中设置了。然后 Django 寻找名为 urlpatterns 变量并且按序匹配正则表达式。在找到匹配项 'polls/',它切掉了匹配的文本("polls/"),将剩余文本——"34/",发送至 'polls.urls' URLconf 做进一步处理。在这里剩余文本匹配了 "<int:question_id>/",使得我们 Django 以如下形式调用 polls.urls.detail():
|
||||
> `detail(request=<HttpRequest object>, question_id=34)`
|
||||
|
|
|
@ -3,5 +3,12 @@ from django.urls import path
|
|||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
# ex: /polls/
|
||||
path('', views.index, name='index'),
|
||||
# ex: /polls/5/
|
||||
path('<int:question_id>/', views.detail, name='detail'),
|
||||
# ex: /polls/5/results/
|
||||
path('<int:question_id>/results/', views.results, name='results'),
|
||||
# ex: /polls/5/vote/
|
||||
path('<int:question_id>/vote/', views.vote, name='vote'),
|
||||
]
|
|
@ -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")
|
||||
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)
|
Loading…
Reference in New Issue