Python - Django - 装饰器版的登陆校验
urls.py:
from django.conf.urls import url
from app01 import views urlpatterns = [
url(r'^login/', views.login),
url(r'^home/', views.home),
url(r'^index/', views.index),
url(r'^logout/', views.logout),
]
views.py:
from django.shortcuts import render, redirect
from app01 import models from functools import wraps # 登录校验的装饰器
def check_login(func):
@wraps(func) # 装饰器修复技术
def inner(request, *args, **kwargs):
ret = request.get_signed_cookie("login", default="0", salt="whoami")
if ret == "success":
# 已经登录过,继续执行
return func(request, *args, **kwargs)
else:
# 否则跳转到登录页面
next_url = request.path_info # 获取当前访问的 URL
# next_url = request.get_full_path() # 获取当前请求的路径和参数
return redirect("/login/?next={}".format(next_url))
return inner def login(request):
if request.method == "POST":
username = request.POST.get("user")
password = request.POST.get("pwd")
next_url = request.GET.get("next") if username == "admin" and password == "admin":
if next_url:
rep = redirect(next_url) # 得到一个响应对象
else:
rep = redirect("/home/") # 得到一个响应对象
# rep.set_cookie("login", "success") # 设置 cookie
rep.set_signed_cookie("login", "success", salt="whoami") # 设置 cookie 并加盐
return rep ret = request.get_signed_cookie("login", default="0", salt="whoami")
if ret == "success":
return redirect("/home/") # 如果已经登录过,再访问 login,直接跳转到 home
else:
return render(request, "login.html") def home(request):
# ret = request.COOKIES.get("login") # 获取 cookie 的 value
ret = request.get_signed_cookie("login", default="0", salt="whoami") # 获取加盐后 cookie 的 value
if ret == "success":
# cookie 验证成功
return render(request, "home.html")
else:
return redirect("/login/") @check_login
def index(request):
return render(request, "index.html") # 注销函数
def logout(request):
rep = redirect("/login/")
rep.delete_cookie("login") # 删除 cookie
return rep
login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body> <p>登录页面</p> <form action="{{ request.get_full_path }}" method="post">
{% csrf_token %}
<p>
账号:
<input type="text" name="user">
</p>
<p>
密码:
<input type="text" name="pwd">
</p>
<p>
<input type="submit" value="登录">
</p>
</form> </body>
</html>
home.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>个人信息页面</title>
</head>
<body> <p>个人信息页面</p> <a href="/logout/">注销</a> </body>
</html>
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主页面</title>
</head>
<body> <p>主页面</p> </body>
</html>
访问,http://127.0.0.1:8888/index/

输入,admin、admin

直接跳转到了 index 页面
这时再访问 login 页面,就会跳转到 home 页面

点击 “注销”

回到了登录界面
Python - Django - 装饰器版的登陆校验的更多相关文章
- Python练习-装饰器版-为什么我的用户总被锁定
参考代码如下: 1.用户登录程序流程控制代码: # 编辑者:闫龙 if __name__ == '__main__': import UserLoginFuncation LoclCount=[]; ...
- 浅谈Django的中间件与Python的装饰器
浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...
- django 使用装饰器验证用户登陆
使用装饰器验证用户登陆,需要使用@method_decorator 首先需引用,method_decorator,并定义一个闭包 from django.utils.decorators import ...
- Python的装饰器实例用法小结
这篇文章主要介绍了Python装饰器用法,结合实例形式总结分析了Python常用装饰器的概念.功能.使用方法及相关注意事项 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让 ...
- 【转】详解Python的装饰器
原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现 ...
- 详解Python的装饰器
Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...
- 【转】【Python】装饰器
1.闭包 >>> def outer(): ... x = 1 ... def inner(): ... ... return inner >>> foo = ou ...
- Python学习---装饰器的学习1210
装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包 [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前 ...
- Python中装饰器(转)
本文由 伯乐在线 - 7even 翻译,艾凌风 校稿.未经许可,禁止转载!英文出处:Simeon Franklin.欢迎加入翻译组. 好吧,我标题党了.作为 Python 教师,我发现理解装饰器是学生 ...
随机推荐
- 接口-httpClient
最近在工作的过程中有遇到httpClient接口,今天特意些一个小示例对这个知识点进行温习. 下面是代码小片段: package com.sinosoft.lis.mgubq.zhaoyongqian ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
- Hdfs的HA高可用
1.Hdfs的HA高可用:保证Hdfs高可用,其实就是保证namenode的高可用,保证namenode的高可用的机制有两个,editlog共享机制+ZKFC.ZKFC就是ZookeeperFailO ...
- YAML_17 Playbook 综合
Playbook1.语法特性如下:(1)"---"首行顶格开始(2)#号注释(3)缩进统一,不同的缩进代表不同的级别,缩进要对齐,空格和tab不能混用(4)区别大小写,键值对k/v ...
- YAML_01 YAML语法和playbook写法
ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...
- npm 安装全局包 不是内部或外部命令的问题
场景: npm已经安装成功 ,通过npm install -g 安装的 全局包 提示不是内部或外部命令 第一步: npm list -g --depth=0:查看npm全局包的路径,和有哪些安装包 ...
- HIVE 乱码以及 HUE SQL 语句兼容性的记录(遇到应该会一直更新)
最近在 HUE 里面查询有中文字段相关的东西被报错警告... (1366, Incorrect string value: \\xE4\\xBA\\xAC\\xE4\\xB8\\x9C... for ...
- Pytest权威教程11-模块及测试文件中集成doctest测试
目录 模块及测试文件中集成doctest测试 编码 使用doctest选项 输出格式 pytest-specific 特性 返回: Pytest权威教程 模块及测试文件中集成doctest测试 编码 ...
- Linux下CFD-Post视图透明的解决方法
今天发生了一件很搞笑的事情,想用CFD-Post对计算结果做后处理,打开CFD-Post之后,背景居然是透明的,见图 做起后处理来完全看不清楚 下面是解决办法,很简单,步骤如下: 在终端中输入 sud ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...