从FBV到CBV二(认证器)
span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}.CodeMirror-sizer {min-height:auto !important;}
-->
li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style-type:lower-latin;}.wiz-editor-body ol.wiz-list-level3 > li {list-style-type:lower-roman;}.wiz-editor-body li.wiz-list-align-style {list-style-position: inside; margin-left: -1em;}.wiz-editor-body blockquote {padding: 0 12px;}.wiz-editor-body blockquote > :first-child {margin-top:0;}.wiz-editor-body blockquote > :last-child {margin-bottom:0;}.wiz-editor-body img {border:0;max-width:100%;height:auto !important;margin:2px 0;}.wiz-editor-body table {border-collapse:collapse;border:1px solid #bbbbbb;}.wiz-editor-body td,.wiz-editor-body th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-word;box-sizing: border-box;}.wiz-editor-body td > div:first-child {margin-top:0;}.wiz-editor-body td > div:last-child {margin-bottom:0;}.wiz-editor-body img.wiz-svg-image {box-shadow:1px 1px 4px #E8E8E8;}.wiz-hide {display:none !important;}
-->
- 认证


'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
)
}<wiz_code_mirror>
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
)
}









- for authenticator in self.authenticators:
通过遍历找到我们的认证器 - user_auth_tuple = authenticator.authenticate(self)
- except exceptions.APIException: self._not_authenticated() raise
- if user_auth_tuple is not None:
self._authenticator = authenticator
self.user, self.auth = user_auth_tuple
return
- self._not_authenticated()


class MyAuthentication(object):
# authenticate authenticate_header 两个方法是必须有的,authenticate用来写我们自己的认证方式,authenticate_header直接写pass就行,不写会抛错,缺少authenticate_header方法
def authenticate(self, request):
self.token = request._request.GET.get('token')
if not self.token:
raise AuthenticationFailed('用户认证失败') # 如果认证失败,就抛出一个AuthenticationFailed异常
return ('wbj', self.token) # 如果认证通过,就行返回一个元组,第一个元素是用户身份(user),第二个是auth
def authenticate_header(self, request):
pass
from rest_framework.exceptions import AuthenticationFailed
class MyAuthentication(object):
# authenticate authenticate_header 两个方法是必须有的,authenticate用来写我们自己的认证方式,authenticate_header直接写pass就行,不写会抛错,缺少authenticate_header方法
def authenticate(self, request):
self.token = request._request.GET.get('token')
if not self.token:
raise AuthenticationFailed('用户认证失败') # 如果认证失败,就抛出一个AuthenticationFailed异常
return ('wbj', self.token) # 如果认证通过,就行返回一个元组,第一个元素是用户身份(user),第二个是auth
def authenticate_header(self, request): # 如果不想写这个方法,可以让MyAuthentication继承于rest_framework.authentication.BaseAuthentication
pass
authentication_classes = [MyAuthentication, ]
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get(self, request):
# get a book
return HttpResponse(json.dumps({'code': '20000'}))
def post(self, request):
return HttpResponse(json.dumps({'code': '20000'}))
def put(self, request):
# update a book
return HttpResponse(json.dumps({'code': '20000'}))
def delete(self, request):
# delete a book
return HttpResponse(json.dumps({'code': '20000'}))
class Book(APIView):
authentication_classes = [MyAuthentication, ]
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get(self, request):
# get a book
return HttpResponse(json.dumps({'code': '20000'}))
def post(self, request):
return HttpResponse(json.dumps({'code': '20000'}))
def put(self, request):
# update a book
return HttpResponse(json.dumps({'code': '20000'}))
def delete(self, request):
# delete a book
return HttpResponse(json.dumps({'code': '20000'}))




从FBV到CBV二(认证器)的更多相关文章
- django的FBV和CBV的装饰器例子
备忘 def auth(func): def inner(request,*args,**kwargs): u = request.COOKIES.get('username111') if not ...
- 一、虚拟环境.二、路由配置主页与404.三、2.x路由分发.四、伪静态.五、request对象.六、FBV与CBV.七、文件上传.
一.虚拟环境 ''' 解决版本共存 1. 用pycharm选择File点击NewProject然后选择virtualenv创建一个纯净环境 2. 打开下载的目录将venv文件夹下的所有文件(纯净的环境 ...
- django基础 -- 4. 模板语言 过滤器 模板继承 FBV 和CBV 装饰器 组件
一.语法 两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二.变量 1. 可直接用 {{ 变量名 }} (可调用字符串, 数字 ,列表,字典,对象等) ...
- diango中的MTV——FBV/CBV以及装饰器的复用问题解决
MVC M: model 模型 与数据库交互 V: view 视图 HTML C:controller 控制器 流程 和 业务逻辑 MTV M:model ORM T:template 模板 HTML ...
- epic游戏平台如何启用认证器应用程序/二次验证码/谷歌身份验证器?
1.登陆epic游戏平台,找到二次验证绑定界面 登陆https://www.epicgames.com/store/zh-CN/, 点右上角用户头像-[账户]. 之后点-[密码与安全] 在[双重验证] ...
- Django FBV和CBV -
一.FBV和CBV 在Python菜鸟之路:Django 路由.模板.Model(ORM)一节中,已经介绍了几种路由的写法及对应关系,那种写法可以称之为FBV: function base view ...
- CBV加装饰器解决登录注册问题和 <<中间件>>
文本目录 CBV加装饰器解决登录注册问题 一:什么是中间件 二:中间件有什么用 三:自定义中间件 四:中间件应用场景 五:SCRF TOKEN跨站请求伪造 六: 其他操作 CBV加装饰器解决登录注册问 ...
- Python菜鸟之路:Django 路由补充1:FBV和CBV - 补充2:url默认参数
一.FBV和CBV 在Python菜鸟之路:Django 路由.模板.Model(ORM)一节中,已经介绍了几种路由的写法及对应关系,那种写法可以称之为FBV: function base view ...
- Django CBV加装饰器、Django中间件、auth模块
一. CBV加装饰器 在视图层中,基于函数的视图叫FBV(function base views),基于类的视图叫CBV(class base views).当需要用到装饰器时,例如之前的基于Cook ...
随机推荐
- Java多线程(2):线程加入/join()
线程加入 join()方法,等待其他线程终止.在当前线程(主线程)中调用另一个线程(子线程)的join()方法,则当前线程转入阻塞状态,直到另一个线程运行结束,当前线程再由阻塞转为就绪状态. 也就是主 ...
- Client Dimensions , offsetHeight , scrollTop 属性详解
http://stackoverflow.com/questions/22675126/what-is-offsetheight-clientheight-scrollheight http://ww ...
- Sqlserver实现故障转移 — 加域(2)
目的:将计算机添加到域中, 域控的建立详见:https://www.cnblogs.com/xiaoerlang90/p/9224745.html 域控: 名称:dcTest.com IP: 192. ...
- Eclipse 4.11 Debug jar包代码时进入空心J
代码调试时,进入jar包中的时候,会出现如下的情况超级影响代码调试 断点打在上面的地方,但是却进入到了空心J的那个地方了. 解决办法:去掉勾选即可. 我是这么解决的.
- 二 MyBatis 从入门到进阶 2 Maven 入门
1 Maven 的使用 1.1 本地仓库与中央仓库 本地仓库:Window \ Preferences \ Maven \ User Settings \ Local Repository 中央仓库: ...
- java.sql.SQLException: Access denied for user 'root'@'10.10.7.180' (using password: YES)
1.刚开始连接数据库提示是: java.sql.SQLException: Access denied for user 'root'@'10.10.7.180' (using password: N ...
- 【有奖征集】报表模板库邀您提反馈,轻松赢取P30!
>>立即参赛 赛事初衷 大数据时代,数据的价值愈发彰显,什么样的报表才能真正帮助业务决策?这几乎是所有信息化建设的企业和个人都在思考的问题. 作为报表领域标杆企业,葡萄城于2017年推出了 ...
- Hive怎么使用远程连接
HIVE的连接模式== 本地连接模式 直接启动hive命令 HIVE的远程连接 这里要启动HIVE的服务 thirft进行编写 hiveserver2 —- > 前台启动 后台启动 前台启动 h ...
- HDU - 1711 A - Number Sequence(kmp
HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...
- jinja2介绍
jinja2介绍 jinja2是Flask作者开发的一个模板系统,起初是仿django模板的一个模板引擎,为Flask提供模板支持,由于其灵活,快速和安全等优点被广泛使用. jinja2的优点 jin ...