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

相关推荐
小宁爱Python4 小时前
从零搭建 RAG 智能问答系统 6:Text2SQL 与工作流实现数据库查询
数据库·人工智能·python·django
言德斐1 天前
Python Web框架深度对比:Django vs Flask vs FastAPI(含优缺点与选型策略)
前端·python·django
belldeep1 天前
python:怎样用 Django 开发电子商务程序
django·电子商务·1024程序员节
清静诗意1 天前
【Django FilterSet 深入解析】类属性过滤器与 Meta.fields 的关系详解(附源码分析)
django·filter
大叔_爱编程1 天前
基于随机森林算法的Boss直聘数据分析及可视化-hadoop+django+spider
hadoop·django·1024程序员节·spider·随机森林算法·boss直聘
白小筠2 天前
创建Django项目
数据库·django·sqlite
计算机毕业设计小帅2 天前
【2026计算机毕业设计】基于Django的社区婴幼儿预防接种系统
数据库·django·课程设计
DokiDoki之父2 天前
SpringMVC—REST风格 & Restful入门案例 & 拦截器简介 & 拦截器入门案例 & 拦截器参数 & 拦截器链配置
后端·restful
李昊哲小课2 天前
spring 中 HttpStatus 与 ResponseEntity
spring boot·后端·spring·http·spring cloud·restful
麦麦大数据3 天前
D027 v2 vue+django+neo4j 基于知识图谱红楼梦问答系统 (新增问关系功能;新增知识节点和关系管理功能,neo4j增删改查功能)
vue.js·django·问答系统·知识图谱·neo4j·图谱管理·neo4j增删改查