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

相关推荐
想要成为糕糕手8 小时前
RESTful Todo 任务清单 —— 从零学习前后端
api·restful·bun
li-xun17 小时前
我给自己的 Django 博客做了一个在线工具箱:从图片压缩到正则测试,尽量都在浏览器本地处理
后端·python·django
Wonderful U17 小时前
Python+Django实战|线上问卷与投票调研系统:自定义题型、问卷发布、链接分享、答卷收集、数据可视化、报表导出
python·信息可视化·django
AIFQuant17 小时前
全球行情自动更新、多品种展示、性能优化实战指南
python·性能优化·金融·node.js·restful
Wonderful U18 小时前
Python+Django实战|社区物业管理系统:业主档案、车位管理、物业费收缴、线上报修、投诉建议、园区公告、日常巡检
android·python·django
山里幽默的程序员2 天前
DevOps 必备:盘点2026 年最强RESTful API 接口测试方案
运维·restful·devops·api开发·api开发工具
不考研当牛马2 天前
Django 框架 深度学习 第二课程
后端·python·django
不考研当牛马3 天前
Django 框架 深度学习
python·深度学习·django
右耳朵猫AI3 天前
Python周刊2026W22 | Django 6.1 Alpha 1发布、Nuitka 4.1发布、PEP 831终稿、PEP 808已接受
开发语言·python·django
Wonderful U3 天前
Python+Django实战|美食菜谱分享与食材采购一体化系统:食谱发布收藏、图文教程、食材商城、购物车、订单管理、美食点评、智能食谱推荐
python·django·美食