django系列3.4-- request对象和response对象(未完待续)

一.request对象

  详细信息能够查阅django官方文档html

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

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.schemedjango

    表示请求方案(http或https 一般)的字符串。cookie

  • request.bodysession

    原始http请求体, 也能够使用相似的方法 request.read()app

  • request.path_infopost

    url的路径信息部分编码

  • request.methodurl

    请求中使用的http请求方法spa

    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:响应的状态码
相关文章
相关标签/搜索