zoukankan      html  css  js  c++  java
  • django系列3.4-- request对象和response对象(未完待续)

    一.request对象

      详细信息可以查阅django官方文档

      共有五种请求相关的常用值

        request.path_info    返回用户访问的url不包括域名
        request.method       请求中使用的HTTP方法的字符串表示,全大写
        request.GET          包含所有HTTP GET参数的类字典对象(request.GET.dict())
        request.POST         包含所有HTTP POST参数的类字典对(request.POST.dict())
        request.body         整个请求体, byte类型 request.POST的数据就是从body里面提取到的
    
    • request.scheme

      表示请求方案(http或https 通常)的字符串。

    • request.body

      原始http请求体, 也可以使用类似的方法 request.read()

    • request.path_info

      url的路径信息部分

    • request.method

      请求中使用的http请求方法

      if request.method == 'GET':
          pass
      elif request.method == 'POST':
          pass
      
    • request.encoding

      当前表单提交数据的编码,可以写入这个属性更改访问表单数据时使用的编码,任何后续属性访问都将使用这个编码

    • request.content_type

      标识请求的MIME类型的字符串,从CONTENT_TYPE标头解析

    • request.content_params

      content_type标题中包含的键/值参数字典

    • request.GET

      一个包含http get请求参数的字典对象

    • request.POST

      包含http post请求参数的字典

    • request.COOKIES

      一个包含所有cookie信息的字典, key,value都是字符串类型

    • request.FILES

      一个包含所有上传文件的类似字典的对象, 每一个key都是<input type='file' name=“”>标签中的name,每个value都是一个上传的文件,只有当请求方法为POST且向请求发送的<form>具有enctype="multipart/form-data"时,文件才会包含数据。否则,文件将是一个类似于字典的空白对象。

    • request.META

      一个包含所有http请求投的字典

    CONTENT_LENGTH – 请求体长度(string).
    CONTENT_TYPE – The MIME type of the request body.
    HTTP_ACCEPT – Acceptable content types for the response.
    HTTP_ACCEPT_ENCODING – Acceptable encodings for the response.
    HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response.
    HTTP_HOST – The HTTP Host header sent by the client.
    HTTP_REFERER – The referring page, if any.
    HTTP_USER_AGENT – The client’s user-agent string.
    QUERY_STRING – The query string, as a single (unparsed) string.
    REMOTE_ADDR – The IP address of the client.
    REMOTE_HOST – The hostname of the client.
    REMOTE_USER – The user authenticated by the Web server, if any.
    REQUEST_METHOD – A string such as "GET" or "POST".
    SERVER_NAME – The hostname of the server.
    SERVER_PORT – The port of the server (as a string).
    

    django本身没有设置但可以设置的属性

    request.current_app  # 模板语言中将使用其值作为current_app参数来reverse()。
    request.urlconf  # 用作当前请求的根URLconf,覆盖ROOT_URLCONF设置
    

    通过中间件设置的属性

    request.session  #  SessionMiddleware
    request.site  #  CurrentSiteMiddleware
    request.user  #  AuthenticationMiddleware

    二.response对象

      引入方式

    	from django.http import HttpResponse
    

      使用

      1.传递字符串

          response = HttpResponse("OK!")
          response = HttpResponse("OK!", content_type="text/plain")    

      2.设置或删除响应头信息

    	response = HttpResponse()
    	response["Content-Type"] = "text/html"; charset='UTF-8'
    

      

      3.属性

            HttpResponse.content:响应内容
            HttpResponse.charset:响应内容的编码
            HttpResponse.status_code:响应的状态码
    

      

  • 相关阅读:
    I/O流
    课堂测试
    1021课堂内容
    课堂jsp
    课堂动手动脑
    从小工到专家读后感
    课堂测试2
    回文课堂测试
    一周进度条博客
    Echart图表
  • 原文地址:https://www.cnblogs.com/robertx/p/10466096.html
Copyright © 2011-2022 走看看