快速入手-基于Django-rest-framework的自身组件权限认证(九)

1、在对应的视图函数里增加认证(局部起作用,不全局生效)

导入类:

from rest_framework.authentication import (

BasicAuthentication,

SessionAuthentication,

)

from rest_framework.permissions import IsAuthenticated, AllowAny

2、基于第八节内容增加以下内容

authentication_classes = [SessionAuthentication]

permission_classes = [IsAuthenticated]

3、完整code

python 复制代码
from django.shortcuts import render, HttpResponse
from rest_framework.response import Response
from rest_framework.decorators import action

from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import (
    ListModelMixin,
    CreateModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
    DestroyModelMixin,
)
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers

from rest_framework.authentication import (
    BasicAuthentication,
    SessionAuthentication,
)
from rest_framework.permissions import IsAuthenticated, AllowAny


from .models import *
from api.serializer import *


# 这种写法实现所有的增删改查,不能够单独进行操作
# class Linkapi(ModelViewSet):
# 不仅可以实现所有的增删改查,而且可以单独也可以全部包含增删改查
class Linkapi(
    GenericViewSet,
    ListModelMixin,
    CreateModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
    DestroyModelMixin,
):

    queryset = Link.objects.all()
    serializer_class = LinkSerializer
    authentication_classes = [SessionAuthentication]
    permission_classes = [IsAuthenticated]

    # 在原有的二级路由中自定义添加三级路由路径
    # 访问路径/api/linkapi/{pk}/login/
    @action(
        methods=["get", "POST"],
        detail=True,
        url_path="login",
    )
    def login(self, request, pk):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

    # detail为false表示路径名格式应该为/api/linkapi/get_new_5/
    @action(
        methods=[
            "get",
        ],
        detail=False,
    )
    def get_new_2(self, request):
        obj = Link.objects.all().filter()[:2]
        serializer = self.get_serializer(instance=obj, many=True)
        return Response(serializer.data)

4、测试

5、仅允许查询,其他方式请求未授权不能访问

导入包

from rest_framework.permissions import (

IsAuthenticated,

AllowAny,

IsAuthenticatedOrReadOnly,

)

修改视图类内容

IsAuthenticated 授权登录后可以访问

IsAuthenticatedOrReadOnly 只允许查询

permission_classes = [IsAuthenticatedOrReadOnly]

6、

相关推荐
LSL666_41 分钟前
7 RESTful 规范
后端·restful
Sammyyyyy1 小时前
Django 6.0 发布,新增原生任务队列与 CSP 支持
数据库·后端·python·django·sqlite·servbay
高洁011 小时前
智能体大模型时代的AI革新者
人工智能·深度学习·算法·机器学习·django
JAVA+C语言1 小时前
Python+Django 核心介绍
开发语言·python·django
qq_2290580120 小时前
运行djando项目 配置启动类 label_studio包含前后端启动方法
python·django
码界奇点1 天前
基于Python与Django的白泽自动化运维系统设计与实现
运维·python·django·毕业设计·源代码管理
计算机毕业编程指导师1 天前
【Python大数据选题】基于Spark+Django的电影评分人气数据可视化分析系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·计算机·spark·django·电影评分人气
Python极客之家1 天前
基于Django的高校二手市场与社交系统
后端·python·数据挖掘·django·毕业设计
码界奇点1 天前
基于Django与Zabbix集成的运维故障管理系统设计与实现
运维·django·毕业设计·zabbix·源代码管理
码界奇点1 天前
基于Django与Ansible的自动化运维管理系统设计与实现
运维·python·django·毕业设计·ansible·源代码管理