FBV

响应方式
http协议,分为http请求与http响应,http响应内容也叫做http状态码,其中可以分为成功,重定向,异常响应(请求错误,和服务器错误)
我们在视图函数中使用return方式返回相应的内容,
return HTTPResponse(响应内容) --200,
return HTTPResponseRedict('url') --302 重定向
return HTTPResponseBadRequest('400') --400,访问页面不存在或者错误
return HTTPResponseNotFound('404') -- 访问页面不存在或者url失效
return JsonResponse(响应内容) 响应内容为json数据,默认值200
return StreamingHttpResponse() 响应内容以流的方式输出,一般用于图片,文件等数据
服务器成功接收,不同的响应方式是服务器高数浏览器当前请求发生了什么事情

render与redirect

render(request,template_name,context=None,content_type=None,status=None,using=None)
python中locals()函数会把全局变量自动写出字典形式,这样我们context就不用之间构造了
redirect() 不仅支持路由地址,还至此路由名字的传入
文件的下载
HttpResponse实现文件下载
	r = HttpResponse(open(file_path,'rb'))
	r['content_type'] = 'application/octet-stream'
	r['Content-Disposition'] = "attachment;filename='下载后的文件名'"
	return r
原理:首先将文件读取并且载入内存,然后在浏览器上实现下载功能
SteamingHttpResponse下载文件
		r = SteamingHttpResponse(open(file_path,'rb'))
		r['content_type'] = 'application/octet-stream'
		r['Content-Disposition'] = "attachment;filename='下载后的文件名'"

FileResponse下载文件
	 r = FileResponse(open(file_path,'rb'),as_attachment=True,filename='下载的文件名')
	 retuen r
HTTP请求对象
在Django中如果接收到http请求后,他会根据请求的信息创建一个WSGIrequest对象,并且作为视图函数的第一个参数request
常见的request对象的属性与方法
	属性
		request.COOKIE  获取 Cookie信息
		request.FILES 获取
		request.GET 获取GET请求参数
		request.POST 获取POST求取参数,用于获取表单数据
		request.body 获取json数据
		request.META 获取请求头中的所有的信息
		request.method 获取请求方式
		request.path 获取请求的 路由地址
		request.session 用于操作服务器的对话信息
		request.user 内置的数据模型User对象
	
	方法
		request.is_secure() 是否采用的是HTTPS协议
		request.is_ajax() 是否采用Ajax发送的请求
		request.get_post() 获取服务器域名
		request.get_full_path()  获取路由地址(不包括域名和端口)
		request.get_raw_uri() 获取完整的网址信息(包括域名和端口)
文件的上传功能
前端中form中要写enctype="multipart/form-data",如果没写我们就不能通过FILES属性在获取文件信息了,而是POST来获取
eg :delete_cookie()
file_object = request.FILES.get('文件名',None)
if file_object:
	file = open(path,wb+)
	for chunk in file_object.chunks:
		file.write(chunk)
文件的基本信息获取
	file_object.name 获取文件名字
	file_object.size  获取文件的大小
	file_object.content_type 获取文件类型
	file_object.read() 读取整个上传的文件,适合小文件上传
	file_object.chunks() 响应式读取文件,适合大文件 上传
	file_object.multiple_chunks() 判断文件的大小(2.5M为参照物),用于判断使用哪种方式上传文件
cookie的响应式布局
response.set_cooki()
response.delete_cookie()
装饰器的使用首先我们先写一个装饰器,登入装饰器
from django.shortcuts import redirect
def auth(func):
    def inner(request,*args,**kwargs):
        v = request.COOKIES.get('username')
        if not v:
            return redirect('/login/')
        return func(request,*args,**kwargs)
    return inner
FBV就相当于直接函数一样使用,在函数前加上@auth

CBV的使用,视图函数以类的形式写,如果想要在对get或者post方式做装饰,在前面加上 @method_decorator(auth),如果相对其中都做装饰,在类前或者在display方法前添加
from django import views
from django.utils.decorators import method_decorator

#@method_decorator(auth,name='dispatch')
class Order(views.View):
  # @method_decorator(auth)
    def dispatch(self, request, *args, **kwargs):
        return super(Order,self).dispatch(request, *args, **kwargs)

    @method_decorator(auth)
    def get(self, reqeust):
        return render(reqeust, 'index.html',)

    def post(self, reqeust):
        return render(reqeust, 'index.html',})
posted @ 2021-01-22 13:54  大魔头的取经故事  阅读(419)  评论(0编辑  收藏  举报