快速入手-基于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、

相关推荐
龙腾AI白云16 小时前
中国人工智能培训网
人工智能·django·知识图谱
带带弟弟学爬虫__19 小时前
dyAPP数据采集-个人主页、发布、搜索、评论
服务器·python·算法·flutter·java-ee·django
IT策士19 小时前
Django 从 0 到 1 打造完整电商平台:登录与登出功能实现
数据库·django·sqlite
lolo大魔王21 小时前
Go 语言 HTTP 协议与 RESTful API 实训全解(理论 + 实战 + 规范)
http·golang·restful
IT策士21 小时前
Django 从 0 到 1 打造完整电商平台:个人中心与用户信息修改
后端·python·django
向上的车轮1 天前
NestJS、Spring Cloud、FastAPI、Django 深度对比分析报告
spring cloud·django·fastapi
前端白袍2 天前
代码规范:RESTful API 全面介绍
后端·restful·代码规范
IT策士3 天前
Django 从 0 到 1 打造完整电商平台:Admin 后台管理与数据初始化
数据库·django·sqlite
IT策士3 天前
Django 从 0 到 1 打造完整电商平台:Django 模型进阶与数据迁移
python·django·sqlite
福大大架构师每日一题3 天前
ragflow v0.25.4 版本更新:RESTful API 数据源连接器、Agent 标签管理、Widget 持久化、GPT-5.4 模型支持全面升级
网络·gpt·restful