快速入手-基于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白云18 小时前
多模大模型应用实战:智能问答系统开发
python·机器学习·数据分析·django·tornado
程序媛徐师姐20 小时前
Python基于OpenCV的马赛克画的设计与实现【附源码、文档说明】
python·opencv·django·马赛克绘画·python马赛克绘画系统·马赛克画·python马赛克画
斯班奇的好朋友阿法法1 天前
Django 项目打包部署完整指南(适配你的项目,零报错)
python·django·sqlite
斯班奇的好朋友阿法法1 天前
Django 3.2 项目:从 Hello World 开始(完整功能版)
python·django
麦聪聊数据2 天前
企业数据流通与敏捷API交付实战(六):内部API门户与自助分发机制
数据库·低代码·restful·etl
源码之屋2 天前
计算机毕业设计:Python天气数据采集与可视化分析平台 Django框架 线性回归 数据分析 大数据 机器学习 大模型 气象数据(建议收藏)✅
人工智能·python·深度学习·算法·django·线性回归·课程设计
架构师老Y2 天前
003、Python Web框架深度对比:Django vs Flask vs FastAPI
前端·python·django
暴力袋鼠哥3 天前
基于 Django 与 Vue 的汽车数据分析系统设计与实现
vue.js·django·汽车
360智汇云3 天前
PostgreSQL 全文检索深度指南:内置 FTS、zhparser 与 pg_search 全解
postgresql·django·全文检索
leo_messi944 天前
2026版商城项目(三)-- ES+认证服务
后端·python·django