八.视图 view
概述:views.py定义的python函数,它接受Web请求并且返回Web响应。
有几个页面就有几个视图view
user出入url地址,发送request--->urls调度器匹配---->views 去model拿数据---->model处理----> views试图响应数据回传--->Template---> user
配置工程路由,在应用下新建urls.py
1.修改工程下的urls.py
使用:正则匹配服务器首页urls调度器
from django.conf.urls import url,include
urlpatterns = [
url('admin/', admin.site.urls),
url(r'^', include("app.urls")),
]
进入应用下的views.py 新建视图函数index:
进行首页视图函数新建:
#from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
import json
def index(request):
data={"user":"test001","msg":"this is test index view "}
js=json.dumps(data)
return HttpResponse(js) 2.在进入应用的目录下新建一个urls.py,并修改配置:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index)
]
urls匹配数字:
views 新建函数视图:
def jobDetail(request,num,num2):
return HttpResponse("THIS IS TESTS REQUEST num of detail {},{}".format(num,num2)) url(r'^(\d+)/(\d+)/$', views.jobDetail),
web 请求: http://127.0.0.1:8000/22/222/ 把数字通过正则匹配传给视图函数
返回:
THIS IS TESTS REQUEST num of detail 22,222