13 权限组件--permissions
权限模块使用方法
全局配置:
# drf的配置
REST_FRAMEWORK = {
# 权限模块的全局配置
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
'rest_framework.permissions.IsAuthenticated',
# 自定义权限类
"api.utils.permissions.SuperUserPermission",
],
}
局部配置:
from api.utils import authentications
class UserListAPIView(ListAPIView):
# 自定义局部配置
permission_classes = [permissions.SuperUserPermission]
自定义权限类的实现方法
- 继承BasePermission类,重写has_permission方法
- 权限规则(has_permission方法实现体):
- 返回True,代表有权限
- 返回False,代表无权限
# 自定义权限类
from rest_framework.permissions import BasePermission
"""
权限模块工作原理
1)继承BasePermission类,重写has_permission方法
2)权限规则(has_permission方法实现体):
返回True,代表有权限
返回False,代表无权限
"""
class SuperUserPermission(BasePermission):
def has_permission(self, request, view):
print(request.user)
print(request.auth)
return request.user and request.user.is_superuser
<input class="dropdown-toggle form-control" data-toggle="dropdown" type="text"
:placeholder="Search"
@focus="search_action" @blur="search_default"
:style="{backgroundColor : search_bg}" v-model="cate">
<ul class="dropdown-menu" v-if="search">
<li>
<router-link to="/category">{{cate}}</router-link>
</li>
</ul>