django之HttpRequest,HttpResponse和render

当一个web请求链接进来时,django会创建一个HttpRequest对象来封装和保存所有请求相关的信息,并且会根据请求路由(urls.py)载入匹配的试图函数,每个请求的试图函数都会返回一个HttpResponse对象。

1. HttpRequest类:

HttpRequest.scheme
请求协议(http或者https)
HttpRequest.body
以字节的方式返回请求体内容;可以通过HttpRequest.POST获取处理后的key和value,也可以通过HttpRequest.read()格式化
HttpRequest.path
返回请求的完整路径,不包括协议和域名
HttpRequest.GET
GET请求参数,返回一个queryDict对象
HttpRequest.POST
获取表单提交的数据,如果是通过POST请求提交的其它非表单数据,可以使用HttpRequest.Body获取;使用时可以通过if request.method == "PSOT"来进行预判断
HttpRequest.method
返回请求方式
HttpRequest.environ
返回一个字典,包含所有django运行的环境信息
HttpRequest.content_type
文件格式
HttpRequest.content_params
参数
HttpRequest.COOKIES
返回一个字典,包含浏览器存储的所有cookie
HttpRequest.FILES
返回一个MultiValueDict,包含上传的文件
HttpRequest.META
返回一个包含所有请求相关信息的字典(包含Headers),同environ
HttpRequest.resolver_match
返回请求处理的url及相关参数
HttpRequest.session
中间件,设置session,一个可读可写的字典对象
HttpRequest.get_host()
获取请求的主机和端口
HttpRequest.get_port()
获取端口
HttpRequest.get_full_path()
返回完整路径,同path
HttpRequest.get_signed_cookie(key, default=RAISE_ERROR, salt=’’, max_age=None)
获取以一个cookie
HttpRequest.is_ajax()
判断是否为ajax请求
HttpRequest.is_secure()
判断是否为https请求

2. HttpResponse类:

from django.http import HttpResponse

实例化一:
responseOne = HttpResponse(“this is a http response”)

实例化二:
responseTwo = HttpResponse()
responseTwo.write(“this is another http response”)
responseTwo.writelines([“this is second line”, “this is third line”])

设置响应头
responseOne[“Age”] = 20
responseOne[“app”] = “sample”
del responseOne[“app”]

设置响应头
responseOne[“content_type”] = ‘application/vnd.ms-excel’
responseOne[‘Content-Disposition’] = ‘attachment; filename=“foo.xls”’

responseOne.set_cookie(“date”, “2018-08-21”, path="/page", ) 设置cookie
responseOne.delete_cookie(“date”) # 删除cookie

有关对象
print(responseOne) # HttpResponse
print(responseOne.items()) # dict_values
print(responseOne.cookies) # cookie
print(responseOne.content) # 内容(字节)
print(responseOne.charset) # 编码
print(responseOne.status_code) # 状态码
print(responseOne.streaming) # 是否为流
print(responseOne.closed) # 是否已发送response
print(responseOne.serialize()) # 序列化响应头和相应内容
print(responseOne.serialize_headers()) # 序列化响应头
print(responseOne.get(“Age”)) # 获取响应头中的某个键值对
print(responseTwo.getvalue()) # 获取相应的内容

将response设置为流数据处理
responseTwo.readable()
responseTwo.seekable()
responseTwo.write("…")

3. render():

结合一个给定的模板和一个给定的上下文字典, 并返回一个渲染后的HttpResponse对象。
参数:
• request: 用于生成响应的请求对象
• template_name: 要使用的模板的完整名称, 可选的参数
• context: 添加到模板上下文的一个字典. 默认是一个空字典. 如果字典中的某个值是可调用的, 视图将在渲染模板之前调用它.
• content_type:  生成的文档要使用的MIME类型. 默认为DEFAULT_CONTENT_TYPE设置的值. 默认为"text/html"
• status: 响应的状态码. 默认为200
• useing: 用于加载模板的模板引擎的名称
在这里插入图片描述

1.HttpResponse

它是作用是内部传入一个字符串参数,然后发给浏览器。

def index(request):
    # 业务逻辑代码
    return HttpResponse("OK")

2、render

render方法可接收三个参数,一是request参数,二是待渲染的html模板文件,三是保存具体数据的字典参数。

它的作用就是将数据填充进模板文件,最后把结果返回给浏览器。与jinja2类似。

def index(request):
    # 业务逻辑代码
    return render(request, "index.html", {"name": "monicx", "hobby": ["reading", "blog"]})

3、redirect

接受一个URL参数,表示让浏览器跳转去指定的URL.

def index(request):
    # 业务逻辑代码
    return redirect("https://blog.csdn.net/miaoqinian")

猜你喜欢

转载自blog.csdn.net/HobbitX/article/details/87896240