- 视图(接收请求返回响应那部分)
CBV(class base view)和FBV(function base view) request对象 1. request.method => 获取请求的方法(GET、POST等) 2. request.GET => 通常用来获取URL里面的参数 127.0.0.1:8000/edit_book/?id=1&name=yimi request.GET => {"id":1, "name":"yimi"} request.GET.get("id") 3. request.POST => 用来获取POST提交过来的数据 request.POST.get("book_name") 4. request.path_info => 获取用户请求的路径(不包含IP和端口和URL参数) 5. request.body => 这个用的比较少 response对象 1. HttpResponse => 返回字符串内容 2. render => 返回一个html页面 3. redirect => 返回一个重定向(告诉浏览器再去访问另外的网址) 4. JsonResponse => 返回一个Json字符串
举例说明
JsonResponse
from django.http import JsonResponse
def main(request):
a = {'a':'1', 'b':'2','c':'3'}
return JsonResponse(a)
CBV
from django.views import View
# CBV model
class cbvtest(View):
def get(self, request):
return HttpResponse(b"ok")
def post(self, request):
pass
url(r'^cbvtest/', views.cbvtest.as_view())