Django-视图层

目录

  1. Request对象
  2. Response对象
  3. JsonResponse对象

Request对象

属性:
request.GET				# HTTP GET请求的所有参数,返回QueryDict
request.POST			# HTTP POST请求的所有参数,返回QueryDict
request.FILES			# 获得上传的文件信息,返回类似字典的对象(请求方法必须为POST且提交的<form>带有enctype="multipart/form-data"才有数据)
request.method			# 获取当前请求方式,返回字符串,且全部是大写
request.body			# HTTP POST原生请求体里的参数内容,返回二进制字节流
request.path			# 获得URL中的路径,返回字符串
request.path_info		# 获取具有 URL 扩展名的资源的附加路径信息,返回字符串
request.scheme			# 获得请求方案(通常是http或https),返回字符串
request.encoding		# 获得提交的数据编码方式,返回字符串
request.COOKIES			# 获得所有的cookie,返回字典
request.session			# 获得当前session,返回类似字典的对象
request.user			# 获得当前用户,返回用户对象,如果未登录, user将设置为 django.contrib.auth.models.AnonymousUser 的一个实例


方法:
request.GET.get(key)			# 获得通过键获得值,返回字符串,值有多个获得最后一个,获取多个使用getlist
request.POST.get(key)			# 获得通过键获得值,返回字符串,值有多个获得最后一个,获取多个使用getlist
request.FILE.get[key]			# 获得通过键获得值,返回文件对象
request.get_host()				# 返回请求的源主机
request.get_full_path			# 返回path(包括附加的查询信息)
request.is_ajax()				# 判断是否ajax请求,是返回True,否返回False
request.is_secure()				# 判断请求是否安全,是返回True,否返回False

Response对象

HttpResponse(str)				# 返回字符串
render(request,html,kwargs)		# 返回HTML文本(第一个参数必须为request,第二个参数为html页面,字符串,第三个参数为字典,传递给页面的参数,也可以用local(),传递函数内的所有变量)
redirect(path)					# 重定向,跳转页面,参数为字符串填写页面路径

JsonResponse对象

JsonResponse(dict)				# 返回json默认只能传递字典,如果要传递其他类型需要设置safe=False

FBV 和 CBV

基于函数的view即FBV,基于类的view即CBV

views的区别
	FBV
	def test(request):
		if request.method == 'POST':
			return HttpResponse('post')
		return HttpResponse('get')
	
	CBV
	导入:from django.views import View			# 
	class Test(View):
		def get(self):
			return HttpResponse('get')
	
		def post(self):
			return HttpResponse('post')

url的区别
	FBV
	url(r'^test/', views.test)
	
	CBV
	url(r'^test/',views.Test.as_view())

装饰器的区别
def warp(func):
    def inner(*args, **kwargs):
        print('start')
        res = func(*args, **kwargs)
        print('end')
        return res
    return inner
	
	FBV
	与正常函数无异
	@warp
	def test(request):
		return HttpResponse('test')

	CBV
	不能将装饰器应用于类的方法中,所有需要将其转换成方法装饰器
	导入转换方法:from django.utils.decorators import method_decorator
	@method_decorator(warp, name='get')								# 方式一:给类添加装饰器,name参数指定需要装饰的方法
	@method_decorator(warp, name='post')
	@method_decorator(warp, name='dispatch')
	class Test(View):
    	@method_decorator(warp)										# 方法二:给dispatch添加装饰器,等于给其他方法添加
	    def dispatch(self, request, *args, **kwargs):
	        return super(Test, self).dispatch(request, *args, **kwargs)

	    @method_decorator(warp)										# 方法三:给单个方法添加装饰器
	    def get(self, request):
	        return HttpResponse('get')
	
	    @method_decorator(warp)
	    def post(self, request):
	        return HttpResponse('post')

ps:
	from django.views.decorators.csrf import csrf_exempt, csrf_protect
	CSRF Token相关装饰器在CBV只能加到dispatch方法上,或者加在视图类上然后name参数指定为dispatch方法。
	csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
	csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值