zoukankan      html  css  js  c++  java
  • orm视图函数层

    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')
    
  • 相关阅读:
    联考20200604 T2 宝石
    联考20200604 T1 旅游
    联考20200603 T2 排列
    [HAOI2017]八纵八横
    联考20200603 T1 解码
    [POI2011]KON-Conspiracy
    CF917D Stranger Trees
    CF1278F Cards
    CF809E Surprise me!
    NOI2016 循环之美
  • 原文地址:https://www.cnblogs.com/jianhaozhou/p/9931218.html
Copyright © 2011-2022 走看看