Django小试身手之视图和模板类

一、视图

  • 在django中,视图对WEB请求进行回应
  • 视图接收reqeust对象作为第一个参数,包含了请求的信息
  • 视图就是一个Python函数,被定义在views.py中
#coding:utf-8
from django.http import HttpResponse

def index(request):
    return HttpResponse("index")
def detail(request,id):
    return HttpResponse("detail %s" % id)
  • 定义完成视图后,需要配置urlconf,否则无法处理请求

1.一般请求先请求index.

一般请求首页的时候,正则里面什么都不写。

在Django中,所有request信息已经被封装好了,但是返回的responce需要我们自己构建。想返回什么,自己去拼接respone就可以了。

正则匹配的时候,是匹配域名后面的,从/admin开始的。

猜你喜欢

转载自blog.csdn.net/m0_37870649/article/details/104374578