1.视图函数的位置形参request的作用
前台Post传过来的数据,包装到POST字典中
request.POST
前台浏览器窗口里携带的数据,包装到GET字典中
request.GET
前台请求的方式
request.method
post提交的数据,body体的内容,前台会封装成:name=lqz&age=18&sex=1
request.body
取出请求的路径,取不到数据部分
print(request.path)
取出请求的路径,能取到数据部分
print(request.get_full_path())
print(request.META)
2.orm视图函数JsonResponse对象
导入的方式:from django.http import JsonResponse
使用方式如下:
from django.http import JsonResponse
def test(request):
dic={'name':'lqz','age':18}
return JsonResponse(dic) //注意目前JsonResponse只能发送字典形式的数据
自定义函数发送json数据
import json
def test(request):
ll = ['name', 'age']
return HttpResponse(json.dumps(ll))
3.CBV和FBV
CBV就是基于类的函数
1.路由层写法
url(r'^test/', views.Test.as_view()),
2.视图函数层写法
from django.views import View
class Test(View):
def get(self, request):#一定要传request对象
return HttpResponse('get-test')
def post(self, request):
return HttpResponse('post-test')