zoukankan      html  css  js  c++  java
  • django框架之视图层等相关内容-64

    1 视图层之请求对象

    def index(request):
       '''
      request:django封装的对象,它的类是WSGIRequest,它里面包含了所有http请求的东西
      '''
       print(request)
       print(type(request))
       # from django.core.handlers.wsgi import WSGIRequest
       #######################1 讲过的
       print(request.method)
       print(request.GET)
       print(request.POST)

       ########################2 新讲的path,get_full_path,META,FIELS,body
       # 自定制请求头
       # 上传文件使用的编码方式是form-data,默认编码方式urlencoded
       print(request.is_ajax()) # 是不是ajax请求
       print(request.path)      # 请求路径
       print(request.get_full_path()) # 请求全路径,带数据

       # print(request.body)     # 请求体,二进制,如果传文件,这个报错
       '''
      使用form表单,默认情况下数据被转成name=lqz&password=123放到请求体中
      request.POST其实是从body中取出bytes格式的,转成了字典
      requet.GET其实是把路径中?后面的部分拆出来,转成了字典
      '''
       print(request.encoding) # 客户端向服务端传递时,使用的编码方法

       print(request.META)    # 重点,字典,一堆东西,请求用户的ip地址,请求头中数据,用户自定制请求头的数据
       '''
      把请求头的key值部分统一加HTTP_ 并且全部转成大写
      '''
       print(request.META['REMOTE_ADDR'])  # 客户端的ip地址
       print(request.FILES)  # 客户端上传的文件



       ########################3 暂时不用关注(后面会讲)
       print(request.COOKIES) # 空字典
       print(request.session) # session对象
       print(request.user)    # 匿名用户
       return HttpResponse('ok')

     

    2 视图层之响应对象

    ### 重点:JsonResponse的使用(看源码)

    def index(request):
       # 三件套
       # return HttpResponse('ok')
       # return render(request,'index.html',context={'name':'lqz','age':18})
       # return redirect('/home') # 重定向自己的地址,重定向第三方地址,经常跟反向解析一起使用

       # 向客户端返回json格式数据
       # import json
       # res=json.dumps({'name':'刘清政','age':18},ensure_ascii=False)
       # return HttpResponse(res)
       # django内置提供的JsonResponse
       # 本质还是HttpResponse

       # ensure_ascii
       # return JsonResponse({'name':'刘清政','age':18},json_dumps_params={'ensure_ascii':False})
       # safe,转换除字典以外的格式,需要safe=False
       return JsonResponse([11,12,13,'lqz',[1,2,3],{'name':'lqz','age':19}],safe=False)

     

    3 cbv和fbv

    # CBV基于类的视图(Class base view)和FBV基于函数的视图(Function base view)
    # 之前学的全是FBV,写的是视图函数

    # 写视图类(还是写在views.py中)
    ## 第一步,写一个类,继承View
    from django.views import View

    class Index(View):
       def get(self, request):  # 当url匹配成功,get请求,会执行它
           return HttpResponse('ok')

       def post(self,request):
           return HttpResponse('post')
       
    ## 第二步:配置路由
    path('index/', views.Index.as_view()),


    # 前期,全是FBV,后期,drf全是CBV



     

    4 文件上传

    ## html注意编码方式
    <form action="/index/" method="post" enctype="multipart/form-data">

       <p>用户名:<input type="text" name="name"></p>
       <p>密码:<input type="password" name="password"></p>
       <p><input type="file" name="myfile"></p>
       <p><input type="submit" value="提交"></p>
    </form>

    # views.py
    def index(request):
       file=request.FILES.get('myfile')
       # 打开一个空文件,写入
       with open(file.name,'wb') as f:
           for line in file.chunks():
               f.write(line)
       return HttpResponse('文件上传成功')

     

     

    5 postman软件

    模拟发送http请求(控制请求路径,请求方式,请求头,请求体)

     

    6 form表单,提交地址

    # action
    #1 不写,默认向当前地址发送请求
    #2 /index/,向当前域(http://127.0.0.1:8000/)的/index/发送请求
    #3 http://127.0.0.1:8000/index/,向该地址发送请求(可以向第三方服务发送请求)

    # method
    # 1 post:发送post请求(默认编码情况下:以key=value&key=value的形式拼到请求体中)
    # 2 get:发送get请求(以key=value&key=value的形式拼到路径中)
    <form action="/index/" method="post">

       <p>用户名:<input type="text" name="name"></p>
       <p>密码:<input type="text" name="password"></p>
       <p><input type="submit" value="提交"></p>
    </form>

     

    7 Pycharm的自动提示

    from django.core.handlers.wsgi import WSGIRequest
    # pycharm的自动提示
    request=request  # type: WSGIRequest

     

     

     

     

  • 相关阅读:
    Springboot中使用Interceptor(拦截器)
    八大排序之冒泡排序
    八大排序之快速排序
    mysql 用户的增删改与授权
    基于Java8开发接口时,处理Java8中的日期
    Springboot中Filter的使用
    正则校验日期,不考虑闰年和闰月
    正则校验时间,24小时制
    记一下mybatis中foreach循环遇到的一个小问题
    sqlserver中一条语句执行查询与更新
  • 原文地址:https://www.cnblogs.com/usherwang/p/13788532.html
Copyright © 2011-2022 走看看