Django 实现用户认证set_Cookie
当用户通过认证时,set_Cookie(key, value)
request.Cookie.get(key) 如果key不为空,就说明验证通过,否者重新跳转回login登录页面
对于URL
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^tp1/', views.tp1),
url(r'^user_list', views.user_list),
url(r'^login', views.login),
url(r'^Order/', views.Order.as_view()),
url(r'^index/', views.index)
]
对于views文件
user = {'name':'dacengzi', 'password':123}
def login(request):
if request.method == 'GET': #如果进来的方式是GET,直接跳转
return render(request, 'login.html')
elif request.method == 'POST': #如果是POST,说明用户已经上传
username = request.POST.get('username') #获取用户名
password = int(request.POST.get('password')) #获取密码
current_time = datetime.datetime.utcnow() #当前时间
current_time = current_time + datetime.timedelta(seconds=5) #当前时间推迟5秒
if username == user['name'] and password == user['password']: #如果用户名和密码匹配
req = redirect('/index/')
req.set_cookie('correct', username, expires=current_time) #设置Cookie标志位
return req
else:
return render(request, 'login.html')
def auth(func):
def inner(request, *args, **kwargs):
answer = request.COOKIES.get('correct') #接收验证所得的标志位
if not answer: #如果没有收到重新跳转回登录界面
return render(request, 'login.html')
else: # 如果有就执行Order文件
return func(request, *args, **kwargs)
return inner
from django.utils.decorators import method_decorator
from django import views
@method_decorator(auth, name='dispatch') #给dispatch加装饰器,程序一进来就进行验证
class Order(views.View):
def get(self, request):
u = request.COOKIES.get('correct') #获取u,进行传递
return render(request, 'index.html', {'u': u})
def index(request):
return render(request, 'index.html')
对于login文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/login/" method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
Django 实现用户认证set_Cookie的更多相关文章
- COOKIE与SESSION、Django的用户认证、From表单
一.COOKIE 与 SESSION 1.简介 1.cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. ...
- python 全栈开发,Day79(Django的用户认证组件,分页器)
一.Django的用户认证组件 用户认证 auth模块 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中: 如果用户存在于数据库中,然后再验证用户 ...
- 深入一下Django的用户认证和cache
深入一下Django的用户认证和cache 用户认证 首先明白一个概念,http协议是无状态的,也就是每一次交互都是独立的,那如何让服务器和客户端进行有状态的交互呢,现在较为常见的方法就是让客户端在发 ...
- Django--分页器(paginator)、Django的用户认证、Django的FORM表单
分页器(paginator) >>> from django.core.paginator import Paginator >>> objects = ['joh ...
- Django自定义用户认证
自定义一个用户认证 详细参考官方文档: https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#django.contrib.au ...
- 【django之用户认证】
一.auth模块 模块导入 from django.contrib import auth 主要方法如下: 1 .authenticate() 提供了用户认证,即验证用户名以及密码是否正确,一般 ...
- CMDB资产管理系统开发【day25】:Django 自定义用户认证
官方文档:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#substituting-a-custom-user-mode ...
- django - 总结 - 用户认证组件
用户认证组件 from django.contrib import auth 从auth_user表中获取对象,没有返回None,其中密码为密文,使用了加密算法 user = auth.authent ...
- Django组件-用户认证
用户认证 一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1.1 .authenti ...
随机推荐
- C函数说明
输入函数scanf_s() 比如:char d[20];写成scanf_s("%s",d,20); 输出函数printf() 比如:printf("hell ...
- 跳转后全屏,兼容大部分浏览器JavaScript
<!DOCTYPE html> <html> <head> <title>测试</title> </head> <body ...
- HDU4035 Maze(师傅逃亡系列•二)(循环型 经典的数学期望)
When wake up, lxhgww find himself in a huge maze. The maze consisted by N rooms and tunnels connecti ...
- org.hibernate.hql.QueryExecutionRequestException:org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations【异常】
springData学习资料 [http://blog.csdn.net/lw_power/article/details/51296353] [JPA报错]org.springframework.w ...
- ubuntu 安装nodejs/npm
安装发行稳定版 Ubuntu 默认仓库里带有Node.js,版本较旧,这并不是最新版,但是应该很稳定.执行如下命令: 1 2 sudo apt-getupdate sudo apt-getinst ...
- GraphQL和RESTful的区别
GraphQL和RESTful的区别 http://graphql.cn/learn/ https://www.cnblogs.com/Wolfmanlq/p/9094418.html http:// ...
- LeetCode 773. Sliding Puzzle
原题链接在这里:https://leetcode.com/problems/sliding-puzzle/description/ 题目: On a 2x3 board, there are 5 ti ...
- python 数组反序的方法
arr = np.array(some_sequence) reversed_arr = arr[::-1] do_something(arr) look_at(reversed_arr) do_so ...
- Elixir's keyword lists as option parameters
备注: 文章转自:https://www.djm.org.uk/posts/writing-extensible-elixir-with-behaviours-adapters-pluggable-b ...
- 远程复制数据免登录 rsync 和 scp
一.备用机上(用于存放备份的机器) 和 目标机上(需要备份的服务器 ,如 246) 都需要安装 : yum install -y rsync 二.备用机上运行命令: -t rsa Generat ...