django的cbv模式、cbv结合装饰器、csrf_token

本博客转载于https://blog.csdn.net/u013210620/article/details/79176391

使用cbv实现视图

CBV结合装饰器

直接加在视图类上但method_decorator必须传 name 关键字参数

加在CBV视图的get或post方法上

加在dispatch方法上

CSRF Token

 

使用cbv实现视图

from django.views import View
from django.utils.decorators import method_decorator


class LoginView(View):

    def get(self, request):
        return render(request, "login.html")

    def post(self, request):
        user = request.POST.get("name")
        pwd = request.POST.get("pwd")
        if user == "safly" and pwd == "123":
            # 登陆成功
            # 写session
            request.session["user2"] = user
            request.session.set_expiry(5)
            return redirect("/index/")

def index(request):
    return render(request,"index.html")

CBV结合装饰器

直接加在视图类上,但method_decorator必须传 name 关键字参数

from django.views import View
from django.utils.decorators import method_decorator


def wrapper(func):
    @wraps(func)
    def inner(request, *args, **kwargs):
        # 登录校验
        cookie_k = request.session.get("user01", None)
        if cookie_k:
            # 表示已经登录的用户
            ret = func(request, *args, **kwargs)
            return ret
        else:
            # 滚去登录
            return redirect("/login/")

    return inner


class LoginView(View):

    def get(self, request):
        return render(request, "login.html")

    def post(self, request):
        user = request.POST.get("name")
        pwd = request.POST.get("pwd")
        if user == "safly" and pwd == "123":
            # 登陆成功
            # 写session
            request.session["user01"] = user
            # request.session.set_expiry(5)
            return redirect("/index/")


@method_decorator(wrapper, name="get")
class IndexView(View):
    def get(self, request):
        user = request.session.get("user01", "游客")
        return render(request, "index.html", {"user": user})

* 加在CBV视图的get或post方法上*

# @method_decorator(wrapper, name="get")
class IndexView(View):
    @method_decorator(wrapper)
    def get(self, request):
        user = request.session.get("user02", "游客")
        return render(request, "index.html", {"user": user})

* 加在dispatch方法上*

# @method_decorator(wrapper, name="get")
class IndexView(View):
    ## 这么写所有的请求方法都要做登录校验
    @method_decorator(wrapper)
    def dispatch(self, request, *args, **kwargs):
        return super(IndexView,self).dispatch(request,*args,**kwargs)

    # @method_decorator(wrapper)
    def get(self, request):
        user = request.session.get("user04", "游客")
        return render(request, "index.html", {"user": user})

CSRF Token

CSRF Token相关装饰器在CBV只能加到dispatch方法上

备注:

csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件
from django.views.decorators.csrf import csrf_exempt, csrf_protect


class HomeView(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(HomeView, self).dispatch(request, *args, **kwargs)

    def get(self, request):
        return render(request, "home.html")

    def post(self, request):
        print("Home View POST method...")
        return redirect("/index/")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值