diango视图操作步骤
2017-12-26 本文已影响0人
AAA年华
1.1.1 视图
1.1.1.1 视图介绍
1. 在django中,视图对WEB请求进行回应
2. 视图接收reqeust对象作为第一个参数,包含了请求的信息
3. 视图就是一个python函数,被定义在views.py中
4.定义完成视图后,需要配置urlconf,否则无法处理请求
******************views.py******************
from django.http import HttpResponse
def index(request):
return HttpResponse("index")
def detail(request,id):
return HttpResponse("detail %s" % id)
1.1.1.2 URLconf
1. 在django中,定义URLconf包括正则表达式、视图两部分
2. django使用正则表达式匹配请求的URL,一旦匹配成功,则调用应用的视图
3. 注意:只匹配路径部分,即除去域名、参数后的字符串
4. 在test1/urls.py插入booktest,使主urlconf连接到booktest.urls模块
5. 在booktest中的urls.py中添加urlconf
******************插入booktest的urls******************
在test1/urls.py插入booktest,使主urlconf连接到booktest.urls模块
url(r'^', include('booktest.urls’)),
******************添加urlconf******************
在booktest中的urls.py中添加urlconf
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^([0-9]+)/$', views.detail),
]
步骤:
1、先进入项目里面的urls文件

2、在进入app,复制粘贴项目里面的urls到app里面,在app(这里面是nianhua)重写url,写它里面的正则表达式。

3、重写app里面的views,里面传一个参数,一般我们写request,这样识别性高。下面return一个响应的函数。
