CBV装饰校验的三种方式session
代码如下:
from django.shortcuts import render,HttpResponse,redirect
from django.views import View
# Create your views here. def login(request):
if request.method == 'POST':
username = request.POST.get('name')
password = request.POST.get('password')
if username == 'jason' and password == '123':
request.session['name'] = 'jason'
return redirect('/home')
return render(request,'login.html') # 要用装饰器需要导的模块
from functools import wraps
from django.utils.decorators import method_decorator
def login_auth(func):
@wraps(func)
def inner(request,*args,**kwargs):
if request.session.get('name'):
return func(*args,**kwargs)
return redirect('/login')
return inner #第二种 name参数必须指定
@method_decorator(login_auth,name='get')
class MyHome(View):
#第三种 只要是在该类中定义的方法都会被指定
@method_decorator(login_auth)
def dispatch(self, request, *args, **kwargs):
super().dispatch(request,*args,**kwargs)
# 第一种
@method_decorator(login_auth)
def get(self,request):
return HttpResponse('get') def post(self,request):
return HttpResponse('home')
CBV装饰校验的三种方式session的更多相关文章
- AntDesign Form表单字段校验的三种方式
1.使用getFieldDecorator的rules规则 最简单的方法就是使用getFieldDecorator中的rules验证.rules中定义校验规则,message为校验不通过时的提示文字. ...
- Java设置session超时(失效)的三种方式
1. 在web容器中设置(此处以tomcat为例) 在tomcat-6.0\conf\web.xml中设置,以下是tomcat 6.0中的默认配置: <!-- ================= ...
- session处理超时的三种方式
1. 在web容器中设置(此处以tomcat为例) 在tomcat-5.0.28\conf\web.xml中设置,以下是tomcat 5.0中的默认配置: <!-- ========= ...
- Struts2获取Session的三种方式
1.Map<String,Object> session = ActionContext.getContext().getSession(); session.put("cod ...
- 【Struts2】Struts2获取session的三种方式
1.Map<String,Object> map = ActionContext.getContext().getSession(); 2.HttpSession session = S ...
- 设置session超时的三种方式
设置session超时的三种方式 1. 在容器中设置:如在tomcat-7\conf\web.xml中设置 Tomcat默认session超时时间为30分钟,可以根据需要修改,负数或0为不限制sess ...
- session设置存活时间的三种方式
在web容器中设置(此处以tomcat为例)在tomcat-5.0.28\conf\web.xml中设置,以下是tomcat 5.0中的默认配置: [html] view plain copy < ...
- python 全栈开发,Day94(Promise,箭头函数,Django REST framework,生成json数据三种方式,serializers,Postman使用,外部python脚本调用django)
昨日内容回顾 1. 内容回顾 1. VueX VueX分三部分 1. state 2. mutations 3. actions 存放数据 修改数据的唯一方式 异步操作 修改state中数据的步骤: ...
- Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式
Linux就这个范儿 第15章 七种武器 linux 同步IO: sync.fsync与fdatasync Linux中的内存大页面huge page/large page David Cut ...
随机推荐
- 834. Sum of Distances in Tree —— weekly contest 84
Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges a ...
- Python对MySQL进行增删查改
python连接MySQL数据库:pymysql # 测试操作 import pymysql # 打开数据库 db = pymysql.connect("localhost", & ...
- 五分钟看懂抓包神技:DPDK
我是一个网络监控软件,我被开发出来的使命就是监控网络中进进出出的所有通信流量. 一直以来,我的工作都非常的出色,但是随着我监控的网络越来越庞大,网络中的通信流量也变得越来越多,我开始有些忙不过来了,逐 ...
- 重置GrindConrol焦点行FocusedRowHandle
List<model> list=this.CurrentList; var selectModel=tempselectmodel; //找selectModel在list中得位置 va ...
- Spider_基础总结3_BeautifulSoup对象+find()+find_all()
# 本节内容: # 解析复杂的 HTML网页: # 1--bs.find() bs.find_all() tag.get_text() # find_all(tag/tag_list,attribut ...
- C++调用Go方法的字符串传递问题及解决方案
摘要:C++调用Go方法时,字符串参数的内存管理需要由Go侧进行深度值拷贝. 现象 在一个APP技术项目中,子进程按请求加载Go的ServiceModule,将需要拉起的ServiceModule信息 ...
- Makefile 指定源文件目录 make
top=$(CURDIR) SRC_DIR=$(top)/src BUILD_DIR=$(SRC_DIR) src=$(wildcard $(SRC_DIR)/*.c) obj=$(patsubst ...
- idea开发工具下,进行多个线程切换调试
- arm64大服务器安装ubuntu18看不到安装界面
前言 最近在使用arm的大服务器需要用到ubuntu相关的一些东西,在操作系统安装过程中遇到了一些问题 记录 华为鲲鹏服务器 这个默认安装centos的都很顺利,安装ubuntu18最新的,impi就 ...
- Bad magic number ImportError in python
是源码编译里面版本不对,删除掉源码pyc然后重新编译就可以了 find .-name '*.pyc'-delete python -m compileall . 更新历史 why when 创建 20 ...