【django】django RESTFramework前后端分离框架快速入门

目录

一、搭建项目开发环境

1.1 pycharm创建项目

1.2 修改配置settings.py 

1.3 新增 static与staticfiles文件夹

1.4 生成数据表

1.5 创建超级用户

1.6 启动项目

 二、安装REST_Framework

2.1 安装

2.2 配置settings

2.3 重新执行生成数据库脚本

三、修改路由

 四、studyApp应用开发

4.1 修改apps.py

 4.2 创建models.py

4.3 修改admin.py

4.4 生成数据表

4.5 添加库表数据

五、使用ModelSerializer序列化 模型类

5.1 创建serializers文件

5.2 序列化models表中的Course

5.3 带URL的HyperlinkedModelSerializer(非必须)

 六、DRF视图开发RESful API接口

6.1 使用django原生编写API接口(可跳过,不推荐)

6.1.1 django函数式编程Function Based View(不推荐)

6.1.2 django原生类视图Class Based View(不推荐)

6.2 DRF函数式编程Function Based View

6.2.1 编码代码-----》(查询列表+新增)

6.2.2 添加路由

 6.2.3 访问验证

6.2.4 测试新增

6.2.5 查询列表

 6.2.6 编写代码 ----》(修改、删除,查询单个)

6.2.7  添加路由

6.2.8 查询单个

6.2.9 修改单个

6.2.10 删除单个 

6.3 DRF类视图Class Based View

6.3.1 代码

 6.3.2 更新url

6.3.3 测试

6.4 通用类视图Generic Classed Based View

6.4.1 代码

6.3.2 添加路由

6.3.3 测试

6.5 DRF的视图集viewsets

6.5.1 代码

6.5.2 路由urls---》 这个写法与之前路由不一样

6.5.3 测试

 七、DRF的认证(了解即可)

7.1 BasicAuthenrication用户名密码认证

7.2 SessionAuthenrication认证

7.3 TokenAuthenrication认证

7.3.1 使用django manage.py 生成token(测试使用)

7.3.2 通过django的信号机制生成token

7.3.2.1 在视图写函数实现

7.3.2.2 项目urls配置路由

7.3.2.3 后台创建一个用户

 7.3.2.4 调用接口生成token

 7.3.2.5 其他接口使用token

 7.3.2.6 每个方法设置不同认证

八、DRF的权限

8.1 常用的权限类

8.2 设置权限策略

8.3 如何自定义对象级别权限

8.3.1 新建permissons.py文件

8.3.2 导入到视图views.py应用

8.3.3 测试

九、如何使用drf的接口文档 

9.1 如何生成接口文档

9.1.1 安装库

9.1.2 settings配置

 9.1.3 urls配置

9.1.4 访问

9.2 接口文档的使用方法

9.2.1 修改settings

9.2.2 修改urls

 9.2.3 访问

9.2.4 使用


前言:django RESTFramework基础学习整理

一、搭建项目开发环境

1.1 pycharm创建项目

1.2 修改配置settings.py 

ALLOWED_HOSTS = ['*'].
.
.# LANGUAGE_CODE = 'en-us'
# TIME_ZONE = 'UTC'
# USE_TZ = True
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = False
USE_I18N = True
.
.
.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'staticfiles')]

1.3 新增 static与staticfiles文件夹

1.4 生成数据表

这里使用默认的数据库 

python manage.py makemigrationspython manage.py migrate

1.5 创建超级用户

# 创建超级用户
python manage.py createsuperuser1、输入邮箱,随便
2、输入密码
3、再次数据密码 
4、 如果密码太简单会提示, 忽略就点Y

1.6 启动项目

python manage.py runserver

http://127.0.0.1:8000/admin/
输入刚才设置的用户  root  123456,能登录成功就说明用户创建成功

 二、安装REST_Framework

2.1 安装


pip install djangorestframeworkpip install coreapi # 第九章使用pip install pyyaml   # 第九章使用

2.2 配置settings

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','studyApp.apps.StudyappConfig','rest_framework', #rest-framework API'rest_framework.authtoken' # DRF 自带的token认证
]
.
.
.#DRF的全局配置
REST_FRAMEWORK = {'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination','PAGE_SIZE': 10,'DATETIME_FORMAT': '%Y-%m-%d %H:%M:%S','DEFAULT_RENDER_CLASSES': [# 渲染response ,可以不写'rest_framework.renderers.JSONRenderer','rest_framework.renders.BrowsableAPIRender',],'DEFAULT_PARSER_CLASSES': [  # 解析request.data'rest_framework.parsers.JSONParser','rest_framework.parsers.FormParser','rest_framework.parsers.MultiPartParser',],'DEFAULT_PERMISSION_CLASSES': [  # 权限'rest_framework.permissions.IsAuthenticatedOrReadOnly',],'DERAULT_AUTHENTICATION_CLASSES': [  # 认证'rest_framework.authentication.BasicAuthentication','rest_framework.authentication.SessionAuthentication','rest_framework.authentication.TokenAuthentication',]
}

2.3 重新执行生成数据库脚本

python manage.py makemigrationspython manage.py migrate

三、修改路由


from django.contrib import admin
from django.urls import path,includeurlpatterns = [path('admin/', admin.site.urls),path('studyApp/',include('studyApp.urls')),path('api-auth/', include('rest_framework.urls')), # DRF登录退出接口,可点击进去看看
]
登录
http://127.0.0.1:8000/api-auth/login/

 四、studyApp应用开发

4.1 修改apps.py

from django.apps import AppConfigclass StudyappConfig(AppConfig):default_auto_field = 'django.db.models.BigAutoField'name = 'studyApp'verbose_name = '学习APP'

 4.2 创建models.py

from django.db import models
from django.conf import settingsclass Course(models.Model):name = models.CharField(max_length=255, unique=True, help_text='课程名称', verbose_name='课程名称')introduction = models.TextField(help_text='课程介绍', verbose_name='课程介绍')teacher = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,help_text='课程讲师', verbose_name='课程讲师')price = models.DecimalField(max_digits=6,decimal_places=2, help_text='价格', verbose_name='价格')created_at = models.DateTimeField(auto_now_add=True, help_text='创建时间', verbose_name='创建时间')updated_at = models.DateTimeField(auto_now=True, help_text='更新时间', verbose_name='更新时间')class Meta:verbose_name = '课程信息',verbose_name_plural = verbose_nameordering = ['price',]def __str__(self):return self.name

4.3 修改admin.py

将model.py创建的Course字段添加到后台系统,方面后期添加数据

from django.contrib import admin
from .models import Course# Register your models here.
@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):list_display = ('name', 'introduction','teacher','price')serch_fields = list_displaylist_filter = list_display

4.4 生成数据表

python manage.py makemigrationspython manage.py migrate

4.5 添加库表数据

http://127.0.0.1:8000/admin

五、使用ModelSerializer序列化 模型类

5.1 创建serializers文件

5.2 序列化models表中的Course

注意:teacher是外键,复用了django的用户表,外键序列化还可以使用遍历深度

在Meta当中设置depth=1 , 数字越大,深度越大,

# -*- coding: utf-8 -*-
# @Time    : 2024/11/1 9:30
# @Author  : super
# @File    : serializers.py
# @Software: PyCharm
# @Describe:
from rest_framework import serializers
from .models import Course
from django.contrib.auth.models import User  # 导入用户模型"""用户序列化器
"""
class UserSerializer(serializers.ModelSerializer):class Meta:model = Userfields = '__all__'class CourseSerializer(serializers.ModelSerializer):teacher = serializers.ReadOnlyField(source='teacher.username')  # 外键显示名称class Meta:model = Coursefields = '__all__'# fields = ('id', 'name', 'teacher', 'price', 'introduction', 'created_at', 'updated_at')

5.3 带URL的HyperlinkedModelSerializer(非必须)

这里是讲解,后面例子没有使用到,可注解掉

# -*- coding: utf-8 -*-
# @Time    : 2024/11/1 9:30
# @Author  : super
# @File    : serializers.py
# @Software: PyCharm
# @Describe:
from rest_framework import serializers
from .models import Course
from django.contrib.auth.models import User  # 导入用户模型"""用户序列化器
"""
class UserSerializer(serializers.ModelSerializer):class Meta:model = Userfields = '__all__'class CourseSerializer(serializers.HyperlinkedModelSerializer):teacher = serializers.ReadOnlyField(source='teacher.username')  # 外键显示名称class Meta:model = Course       # url 是默认值,可在settings.py设置URL_FIELD_NAME使全局生效fields = ('id', 'url', 'teacher', 'price', 'introduction', 'created_at', 'updated_at')

如果要更url,这样设置,(不建议)

 六、DRF视图开发RESful API接口

6.1 使用django原生编写API接口(可跳过,不推荐)

6.1.1 django函数式编程Function Based View(不推荐)

使用django原生的函数式编写api接口

from django.shortcuts import render
import json
from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt   # 用于免除csrf验证
# Create your views here.
coures_dict = {  "name": "python",  "introduction": "python基础入门",  "price": 100  
}@csrf_exempt
def course_list(request):'''django原生的fbv式编写API接口:param request::return:'''if request.method == 'Get':return JsonResponse(coures_dict)# return HttpResponse(json.dumps(coures_dict), content_type='application/json')  # 这个与上面JsonResponse作用一样if request.method == 'POST':course_data = json.loads(request.body.decode('utf-8'))# return JsonResponse(course_data, safe=False) # 这个与下面的JsonResponse作用一样return HttpResponse(json.dumps(course_data), content_type='application/json')

6.1.2 django原生类视图Class Based View(不推荐)

使用django原生的类编写api接口

import json
from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt   # 用于免除csrf验证
from django.utils.decorators import method_decorator  # 用于方法免除csrf验证
from django.views import View@method_decorator(csrf_exempt, name='dispatch')
class CourseView(View):'''django原生的cbv式编写API接口'''def get(self, request):return JsonResponse(coures_dict)def post(self, request):course_data = json.loads(request.body.decode('utf-8'))return JsonResponse(course_data, safe=False)

6.2 DRF函数式编程Function Based View

6.2.1 编码代码-----》(查询列表+新增)

from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from .models import Course
from .serializers import CourseSerializer"""一、 函数式编程 FBV
"""
@api_view(['GET', 'POST'])
def coures_list(request):'''获取课程列表或新增一个课程:param request::return:'''if request.method == 'GET':courses = CourseSerializer(instance=Course.objects.all(), many=True)return Response(data=courses.data,status=status.HTTP_200_OK)elif request.method == 'POST':serializer = CourseSerializer(data=request.data) # partial=True 只验证部分字段,适用于非必填字段if serializer.is_valid():serializer.save(teacher=request.user) # teacher 字段为外键,获取登录用户id,因为复用了django的用户模型,有认证功能return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)

6.2.2 添加路由

在应用新增urls文件,输入内容

# -*- coding: utf-8 -*-
# @Time    : 2024/11/1 10:27
# @Author  : super
# @File    : urls.py
# @Software: PyCharm
# @Describe:
from django.urls import path,include
from studyApp import viewsurlpatterns = [path('fbv/list/', views.coures_list, name='fbv-list'), # 函数视图FBV]

 6.2.3 访问验证

http://127.0.0.1:8000/studyApp/fbv/list/

因为这里设置了全局的身份认证,需要登录

6.2.4 测试新增

json

{"name": "python","introduction": "python基础入门","price": 100
}

6.2.5 查询列表

 

 6.2.6 编写代码 ----》(修改、删除,查询单个)

from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from .models import Course
from .serializers import CourseSerializer"""一、 函数式编程 FBV
"""
@api_view(['GET', 'POST'])
def coures_list(request):'''获取课程列表或新增一个课程:param request::return:'''if request.method == 'GET':courses = CourseSerializer(instance=Course.objects.all(), many=True)return Response(data=courses.data,status=status.HTTP_200_OK)elif request.method == 'POST':serializer = CourseSerializer(data=request.data) # partial=True 只验证部分字段,适用于非必填字段if serializer.is_valid():serializer.save(teacher=request.user) # teacher 字段为外键,获取登录用户id,因为复用了django的用户模型,有认证功能return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)@api_view(['GET', 'PUT', 'DELETE'])
def course_detail(request, pk):'''获取单个课程详情,更新或删除一个课程:param request::param pk::return:'''''try:course = Course.objects.get(pk=pk)except Course.DoesNotExist:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)if request.method == 'GET':serializer = CourseSerializer(course)return Response(data=serializer.data, status=status.HTTP_200_OK)elif request.method == 'PUT':serializer = CourseSerializer(instance=course, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)elif request.method == 'DELETE':course.delete()return Response(status=status.HTTP_204_NO_CONTENT)else:return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)

6.2.7  添加路由

from django.urls import path,include
from studyApp import viewsurlpatterns = [path('fbv/list/', views.coures_list, name='fbv-list'), # 函数视图FBVpath('fbv/detail/<int:pk>/', views.course_detail, name='fbv-detail'),]

6.2.8 查询单个

http://127.0.0.1:8000/studyApp/fbv/detail/2/

6.2.9 修改单个

请求体

{"name": "菠菜的野蛮生长","introduction": "编码人生","price": 100
}

 

6.2.10 删除单个 

知识点:使用postman测试的话注意这个地方

另外一种设置方式:

 继承

  

6.3 DRF类视图Class Based View

6.3.1 代码

from django.shortcuts import render
from rest_framework.decorators import api_view  # fbv
from rest_framework.views import APIView  #cbv
from rest_framework.response import Response
from rest_framework import status
from .models import Course
from .serializers import CourseSerializer"""一、 函数式编程 FBV
"""
@api_view(['GET', 'POST'])
def coures_list(request):'''获取课程列表或新增一个课程:param request::return:'''if request.method == 'GET':courses = CourseSerializer(instance=Course.objects.all(), many=True)return Response(data=courses.data,status=status.HTTP_200_OK)elif request.method == 'POST':serializer = CourseSerializer(data=request.data) # partial=True 只验证部分字段,适用于非必填字段if serializer.is_valid():serializer.save(teacher=request.user) # teacher 字段为外键,获取登录用户id,因为复用了django的用户模型,有认证功能return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)@api_view(['GET', 'PUT', 'DELETE'])
def course_detail(request, pk):'''获取单个课程详情,更新或删除一个课程:param request::param pk::return:'''''try:course = Course.objects.get(pk=pk)except Course.DoesNotExist:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)if request.method == 'GET':serializer = CourseSerializer(course)return Response(data=serializer.data, status=status.HTTP_200_OK)elif request.method == 'PUT':serializer = CourseSerializer(instance=course, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)elif request.method == 'DELETE':course.delete()return Response(status=status.HTTP_204_NO_CONTENT)else:return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)"""二、 函数式编程 CBV
"""
class CourseList(APIView):"""获取课程列表或新增一个课程"""def get(self, request):queryset = Course.objects.all()serializer = CourseSerializer(instance=queryset, many=True)return Response(data=serializer.data, status=status.HTTP_200_OK)def post(self, request):serializer = CourseSerializer(data=request.data)if serializer.is_valid():serializer.save(teacher=self.request.user)return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)class CourseDetail(APIView):"""获取单个课程详情,更新或删除一个课程"""@staticmethoddef get_object(pk):'''获取课程对象:param self::param pk::return:'''try:return Course.objects.get(pk=pk)except Course.DoesNotExist:returndef get(self, request, pk):obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(obj)return Response(data=serializer.data, status=status.HTTP_200_OK)def put(self, request, pk):'''更新课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(instance=obj, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)def delete(self, request, pk):'''删除课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)obj.delete()return Response(status=status.HTTP_204_NO_CONTENT)

 6.3.2 更新url

# -*- coding: utf-8 -*-
# @Time    : 2024/11/1 10:27
# @Author  : super
# @File    : urls.py
# @Software: PyCharm
# @Describe:
from django.urls import path,include
from studyApp import viewsurlpatterns = [path('fbv/list/', views.coures_list, name='fbv-list'), # 函数视图FBVpath('fbv/detail/<int:pk>/', views.course_detail, name='fbv-detail'), # 函数视图FBVpath('cbv/list/', views.CourseList.as_view(), name='cbv-list'), # 类视图CBVpath('cbv/detail/<int:pk>/', views.CourseDetail.as_view(), name='cbv-detail'), # 类视图CBV]

6.3.3 测试

查询全部

新增 

修改

删除

6.4 通用类视图Generic Classed Based View

6.4.1 代码

如果没有那个teacher ,那个方法重写可以不需要

from django.shortcuts import render
from rest_framework.decorators import api_view  # fbv
from rest_framework.views import APIView  #cbv
from rest_framework import generics # gcb通用视图类
from rest_framework.response import Response
from rest_framework import status
from .models import Course
from .serializers import CourseSerializer"""一、 函数式编程 FBV
"""
@api_view(['GET', 'POST'])
def coures_list(request):'''获取课程列表或新增一个课程:param request::return:'''if request.method == 'GET':courses = CourseSerializer(instance=Course.objects.all(), many=True)return Response(data=courses.data,status=status.HTTP_200_OK)elif request.method == 'POST':serializer = CourseSerializer(data=request.data) # partial=True 只验证部分字段,适用于非必填字段if serializer.is_valid():serializer.save(teacher=request.user) # teacher 字段为外键,获取登录用户id,因为复用了django的用户模型,有认证功能return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)@api_view(['GET', 'PUT', 'DELETE'])
def course_detail(request, pk):'''获取单个课程详情,更新或删除一个课程:param request::param pk::return:'''''try:course = Course.objects.get(pk=pk)except Course.DoesNotExist:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)if request.method == 'GET':serializer = CourseSerializer(course)return Response(data=serializer.data, status=status.HTTP_200_OK)elif request.method == 'PUT':serializer = CourseSerializer(instance=course, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)elif request.method == 'DELETE':course.delete()return Response(status=status.HTTP_204_NO_CONTENT)else:return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)"""二、 函数式编程 CBV
"""
class CourseList(APIView):"""获取课程列表或新增一个课程"""def get(self, request):queryset = Course.objects.all()serializer = CourseSerializer(instance=queryset, many=True)return Response(data=serializer.data, status=status.HTTP_200_OK)def post(self, request):serializer = CourseSerializer(data=request.data)if serializer.is_valid():serializer.save(teacher=self.request.user)return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)class CourseDetail(APIView):"""获取单个课程详情,更新或删除一个课程"""@staticmethoddef get_object(pk):'''获取课程对象:param self::param pk::return:'''try:return Course.objects.get(pk=pk)except Course.DoesNotExist:returndef get(self, request, pk):obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(obj)return Response(data=serializer.data, status=status.HTTP_200_OK)def put(self, request, pk):'''更新课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(instance=obj, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)def delete(self, request, pk):'''删除课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)obj.delete()return Response(status=status.HTTP_204_NO_CONTENT)'''三、 通用视图类
'''class CourseGenericAPIView(generics.ListCreateAPIView):'''获取课程列表或新增一个课程'''queryset = Course.objects.all()serializer_class = CourseSerializerdef perform_create(self, serializer):   # 重写perform_create方法,在保存之前添加teacher字段serializer.save(teacher=self.request.user)class CourseGenericDetailAPIView(generics.RetrieveUpdateDestroyAPIView):'''获取单个课程详情,更新或删除一个课程'''queryset = Course.objects.all()serializer_class = CourseSerializer

6.3.2 添加路由

# -*- coding: utf-8 -*-
# @Time    : 2024/11/1 10:27
# @Author  : super
# @File    : urls.py
# @Software: PyCharm
# @Describe:
from django.urls import path,include
from studyApp import viewsurlpatterns = [path('fbv/list/', views.coures_list, name='fbv-list'), # 函数视图FBVpath('fbv/detail/<int:pk>/', views.course_detail, name='fbv-detail'), # 函数视图FBVpath('cbv/list/', views.CourseList.as_view(), name='cbv-list'), # 类视图CBVpath('cbv/detail/<int:pk>/', views.CourseDetail.as_view(), name='cbv-detail'), # 类视图CBVpath('gcbv/list/', views.CourseGenericAPIView.as_view(),name='gcbv-list'), # 通用视图类gcbvpath('gcbv/detail/<int:pk>/', views.CourseGenericDetailAPIView.as_view(), name='gcbv-detail'),# 通用视图类gcbv]

6.3.3 测试

查询列表

新增

查询单个

修改

删除

6.5 DRF的视图集viewsets

6.5.1 代码

from django.shortcuts import render
from rest_framework.decorators import api_view  # fbv
from rest_framework.views import APIView  #cbv
from rest_framework import generics # gcbv通用视图类
from rest_framework import viewsets # 视图集
from rest_framework.response import Response
from rest_framework import status
from .models import Course
from .serializers import CourseSerializer"""一、 函数式编程 FBV
"""
@api_view(['GET', 'POST'])
def coures_list(request):'''获取课程列表或新增一个课程:param request::return:'''if request.method == 'GET':courses = CourseSerializer(instance=Course.objects.all(), many=True)return Response(data=courses.data,status=status.HTTP_200_OK)elif request.method == 'POST':serializer = CourseSerializer(data=request.data) # partial=True 只验证部分字段,适用于非必填字段if serializer.is_valid():serializer.save(teacher=request.user) # teacher 字段为外键,获取登录用户id,因为复用了django的用户模型,有认证功能return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)@api_view(['GET', 'PUT', 'DELETE'])
def course_detail(request, pk):'''获取单个课程详情,更新或删除一个课程:param request::param pk::return:'''''try:course = Course.objects.get(pk=pk)except Course.DoesNotExist:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)if request.method == 'GET':serializer = CourseSerializer(course)return Response(data=serializer.data, status=status.HTTP_200_OK)elif request.method == 'PUT':serializer = CourseSerializer(instance=course, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)elif request.method == 'DELETE':course.delete()return Response(status=status.HTTP_204_NO_CONTENT)else:return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)"""二、 函数式编程 CBV
"""
class CourseList(APIView):"""获取课程列表或新增一个课程"""def get(self, request):queryset = Course.objects.all()serializer = CourseSerializer(instance=queryset, many=True)return Response(data=serializer.data, status=status.HTTP_200_OK)def post(self, request):serializer = CourseSerializer(data=request.data)if serializer.is_valid():serializer.save(teacher=self.request.user)return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)class CourseDetail(APIView):"""获取单个课程详情,更新或删除一个课程"""@staticmethoddef get_object(pk):'''获取课程对象:param self::param pk::return:'''try:return Course.objects.get(pk=pk)except Course.DoesNotExist:returndef get(self, request, pk):obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(obj)return Response(data=serializer.data, status=status.HTTP_200_OK)def put(self, request, pk):'''更新课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(instance=obj, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)def delete(self, request, pk):'''删除课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)obj.delete()return Response(status=status.HTTP_204_NO_CONTENT)'''三、 通用视图类
'''class CourseGenericAPIView(generics.ListCreateAPIView):'''获取课程列表或新增一个课程'''queryset = Course.objects.all()serializer_class = CourseSerializerdef perform_create(self, serializer):   # 重写perform_create方法,在保存之前添加teacher字段serializer.save(teacher=self.request.user)class CourseGenericDetailAPIView(generics.RetrieveUpdateDestroyAPIView):'''获取单个课程详情,更新或删除一个课程'''queryset = Course.objects.all()serializer_class = CourseSerializer'''四、 视图集
'''
class CourseViewSet(viewsets.ModelViewSet):queryset = Course.objects.all()serializer_class = CourseSerializerdef perform_create(self, serializer):  # 重写perform_create方法,在保存之前添加teacher字段serializer.save(teacher=self.request.user)

6.5.2 路由urls---》 这个写法与之前路由不一样

# -*- coding: utf-8 -*-
# @Time    : 2024/11/1 10:27
# @Author  : super
# @File    : urls.py
# @Software: PyCharm
# @Describe:
from django.urls import path,include
from studyApp import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(prefix='courseviewsets', viewset=views.CourseViewSet)urlpatterns = [# 函数视图FBVpath('fbv/list/', views.coures_list, name='fbv-list'), # 函数视图FBVpath('fbv/detail/<int:pk>/', views.course_detail, name='fbv-detail'), # 函数视图FBV# 类视图CBVpath('cbv/list/', views.CourseList.as_view(), name='cbv-list'), # 类视图CBVpath('cbv/detail/<int:pk>/', views.CourseDetail.as_view(), name='cbv-detail'), # 类视图CBV# 通用视图类gcbvpath('gcbv/list/', views.CourseGenericAPIView.as_view(),name='gcbv-list'), # 通用视图类gcbvpath('gcbv/detail/<int:pk>/', views.CourseGenericDetailAPIView.as_view(), name='gcbv-detail'),# 通用视图类gcbv# 视图集写法不一样path('', include(router.urls)),
]

6.5.3 测试

列表

新增

查询单个

 

修改单个

删除单个 

 七、DRF的认证(了解即可)

前面配置的三种认证,这里了解即可,一般使用jwt,博主其他博文有介绍

7.1 BasicAuthenrication用户名密码认证

这个认证一般用于测试,不要在生产环境使用

7.2 SessionAuthenrication认证

使用的是django默认的后端会话, 

7.3 TokenAuthenrication认证

这个要配置,之前配置了

 之前执行数据同步将对应库表更新到了数据库,双击这个默认数据库

7.3.1 使用django manage.py 生成token(测试使用)

python manage.py drf_create_token root  
# root  是对应用户

在上面的库表可见

7.3.2 通过django的信号机制生成token

7.3.2.1 在视图写函数实现
'''
token 认证
'''
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from rest_framework.authtoken.models import Token@receiver(post_save, sender=settings.AUTH_USER_MODEL) # django  信号
def generate_token(sender, instance=None, created=False, **kwargs):'''创建用户生成token:param sender::param instance::param created::param kwargs::return:'''if created:Token.objects.create(user=instance)

7.3.2.2 项目urls配置路由

"""
URL configuration for djangoRESTFramework project.The `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views1. Add an import:  from my_app import views2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views1. Add an import:  from other_app.views import Home2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from rest_framework.authtoken import views urlpatterns = [path('api-token-auth/', views.obtain_auth_token), # token认证path('admin/', admin.site.urls),path('studyApp/',include('studyApp.urls')),path('api-auth/', include('rest_framework.urls')), # DRF登录退出接口,可点击进去看看
]
7.3.2.3 后台创建一个用户
bocai
bocai@163.com

 7.3.2.4 调用接口生成token

 7.3.2.5 其他接口使用token

 7.3.2.6 每个方法设置不同认证

 方法认证优先级大于全局settings

函数视图添加方法

类视图\通用类、视图集添加方法相同

from django.shortcuts import render
from rest_framework.decorators import api_view, authentication_classes # fbv
from rest_framework.views import APIView  #cbv
from rest_framework import generics # gcbv通用视图类
from rest_framework import viewsets # 视图集
from rest_framework.response import Response
from rest_framework import status
from .models import Course
from .serializers import CourseSerializer
from rest_framework.authentication import BasicAuthentication,SessionAuthentication,TokenAuthentication'''
token 认证
'''
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from rest_framework.authtoken.models import Token@receiver(post_save, sender=settings.AUTH_USER_MODEL) # django  信号
def generate_token(sender, instance=None, created=False, **kwargs):'''创建用户生成token:param sender::param instance::param created::param kwargs::return:'''if created:Token.objects.create(user=instance)"""一、 函数式编程 FBV
"""
@api_view(['GET', 'POST'])
@authentication_classes((BasicAuthentication,TokenAuthentication))
def coures_list(request):'''获取课程列表或新增一个课程:param request::return:'''if request.method == 'GET':courses = CourseSerializer(instance=Course.objects.all(), many=True)return Response(data=courses.data,status=status.HTTP_200_OK)elif request.method == 'POST':serializer = CourseSerializer(data=request.data) # partial=True 只验证部分字段,适用于非必填字段if serializer.is_valid():serializer.save(teacher=request.user) # teacher 字段为外键,获取登录用户id,因为复用了django的用户模型,有认证功能return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)@api_view(['GET', 'PUT', 'DELETE'])
@authentication_classes((BasicAuthentication,TokenAuthentication))
def course_detail(request, pk):'''获取单个课程详情,更新或删除一个课程:param request::param pk::return:'''''try:course = Course.objects.get(pk=pk)except Course.DoesNotExist:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)if request.method == 'GET':serializer = CourseSerializer(course)return Response(data=serializer.data, status=status.HTTP_200_OK)elif request.method == 'PUT':serializer = CourseSerializer(instance=course, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)elif request.method == 'DELETE':course.delete()return Response(status=status.HTTP_204_NO_CONTENT)else:return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)"""二、 函数式编程 CBV
"""
class CourseList(APIView):"""获取课程列表或新增一个课程"""authentication_classes = (BasicAuthentication,SessionAuthentication,TokenAuthentication)def get(self, request):queryset = Course.objects.all()serializer = CourseSerializer(instance=queryset, many=True)return Response(data=serializer.data, status=status.HTTP_200_OK)def post(self, request):serializer = CourseSerializer(data=request.data)if serializer.is_valid():serializer.save(teacher=self.request.user)return Response(data=serializer.data, status=status.HTTP_201_CREATED)return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)class CourseDetail(APIView):"""获取单个课程详情,更新或删除一个课程"""authentication_classes = (BasicAuthentication, SessionAuthentication, TokenAuthentication)@staticmethoddef get_object(pk):'''获取课程对象:param self::param pk::return:'''try:return Course.objects.get(pk=pk)except Course.DoesNotExist:returndef get(self, request, pk):obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(obj)return Response(data=serializer.data, status=status.HTTP_200_OK)def put(self, request, pk):'''更新课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)serializer = CourseSerializer(instance=obj, data=request.data)if serializer.is_valid():serializer.save()return Response(serializer.data, status=status.HTTP_200_OK)return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)def delete(self, request, pk):'''删除课程:param request::param pk::return:'''obj = self.get_object(pk=pk)if obj is None:return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)obj.delete()return Response(status=status.HTTP_204_NO_CONTENT)'''三、 通用视图类
'''class CourseGenericAPIView(generics.ListCreateAPIView):'''获取课程列表或新增一个课程'''queryset = Course.objects.all()serializer_class = CourseSerializerdef perform_create(self, serializer):   # 重写perform_create方法,在保存之前添加teacher字段serializer.save(teacher=self.request.user)class CourseGenericDetailAPIView(generics.RetrieveUpdateDestroyAPIView):'''获取单个课程详情,更新或删除一个课程'''queryset = Course.objects.all()serializer_class = CourseSerializer'''四、 视图集
'''
class CourseViewSet(viewsets.ModelViewSet):queryset = Course.objects.all()serializer_class = CourseSerializerdef perform_create(self, serializer):  # 重写perform_create方法,在保存之前添加teacher字段serializer.save(teacher=self.request.user)

八、DRF的权限

8.1 常用的权限类

8.2 设置权限策略

函数视图添加方法

类视图\通用类、视图集添加方法相同 

8.3 如何自定义对象级别权限

8.3.1 新建permissons.py文件

# -*- coding: utf-8 -*-
# @Time    : 2024/11/1 15:58
# @Author  : super
# @File    : permissions.py
# @Software: PyCharm
# @Describe:
from rest_framework import permissionsclass IsOwnerReadOnly(permissions.BasePermission):"""自定义权限只允许对象的所有者编辑它。"""def has_object_permission(self, request, view, obj):"""判断用户是否有权限访问该对象。:param request::param view::param obj::return: bool"""if request.method in permissions.SAFE_METHODS:return True# 判断当前用户是否是该对象的拥有者return obj.teacher == request.user

8.3.2 导入到视图views.py应用

这里是修改

8.3.3 测试

这个id=9 是root创建的

 那么bocai无法修改

九、如何使用drf的接口文档 

9.1 如何生成接口文档

9.1.1 安装库

前面安装就可不安装

pip install coreapi
pip install pyyaml

9.1.2 settings配置

'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',

 9.1.3 urls配置

"""
URL configuration for djangoRESTFramework project.The `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views1. Add an import:  from my_app import views2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views1. Add an import:  from other_app.views import Home2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from rest_framework.authtoken import views
from rest_framework.schemas import get_schema_view
schema_view = get_schema_view(title='DRF API文档',description="API", version="0.1")urlpatterns = [path('api-token-auth/', views.obtain_auth_token), # token认证path('admin/', admin.site.urls),path('studyApp/',include('studyApp.urls')),path('api-auth/', include('rest_framework.urls')), # DRF登录退出接口,可点击进去看看path('schema/', schema_view),
]

9.1.4 访问

http://127.0.0.1:8000/schema/

谷歌浏览器

9.2 接口文档的使用方法

9.2.1 修改settings

'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',

9.2.2 修改urls

"""
URL configuration for djangoRESTFramework project.The `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views1. Add an import:  from my_app import views2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views1. Add an import:  from other_app.views import Home2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from rest_framework.authtoken import views
from rest_framework.documentation import include_docs_urls
# from rest_framework.schemas import get_schema_view
# schema_view = get_schema_view(title='DRF API文档',description="API", version="0.1")urlpatterns = [path('api-token-auth/', views.obtain_auth_token), # token认证path('admin/', admin.site.urls),path('studyApp/',include('studyApp.urls')),path('api-auth/', include('rest_framework.urls')), # DRF登录退出接口,可点击进去看看# path('schema/', schema_view),path('docs/',include_docs_urls(title='DRF API 文档',description='Django REST Framwork 快速入门')),
]

 9.2.3 访问

http://127.0.0.1:8000/docs/

9.2.4 使用

注意:配置认证页面不能刷新

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/58649.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【微服务】Java 对接飞书多维表格使用详解

目录 一、前言 二、前置操作 2.1 开通企业飞书账户 2.2 确保账户具备多维表操作权限 2.3 创建一张测试用的多维表 2.4 获取飞书开放平台文档 2.5 获取Java SDK 三、应用App相关操作 3.1 创建应用过程 3.2 应用发布过程 3.3 应用添加操作权限 四、多维表应用授权操作…

二维legendre多项式

Legendre 多项式常用来表征方形波前的畸变。 目录 一维legendre多项式正交性自正交性 二维Legendre多项式正交性证明 可视化二维 Legendre 多项式解释 Legendre拟合方法1MATLAB 实现解释方法21. 定义一维 Legendre 多项式函数2. 生成二维 Legendre 多项式矩阵3. 计算 Legendre…

台式电脑如何改ip地址:全面解析与实操指南

有时候&#xff0c;由于IP地址冲突、网络安全、隐私保护或特定应用需求&#xff0c;我们可能需要更改台式电脑的IP地址。然而&#xff0c;对于不熟悉网络设置的用户来说&#xff0c;这一过程可能显得复杂而陌生。本文将通过全面解析与实操指南&#xff0c;帮助大家轻松掌握台式…

【私聊记录】最近在忙什么啊?听说你在学人工智能?

小舒&#xff1a;哎&#xff0c;你最近在忙什么啊&#xff1f; 小元&#xff1a;我在学习人工智能呢。 小舒&#xff1a;人工智能&#xff1f;难不难学啊&#xff1f; 小元&#xff1a;不难&#xff0c;找到正确的学习姿势就不难了&#xff01; 小舒&#xff1a;那你为什么想学…

电动越野车行业全面深入分析

电动越野摩托车是将电动技术与越野性能相结合的一类摩托车&#xff0c;采用电力驱动&#xff0c;具有环保、节能、低噪音等优点&#xff0c;同时保留了越野摩托车的强劲动力和适应复杂地形的能力。电动越野摩托车通常配备高性能电动机和电池组&#xff0c;可提供强劲的动力输出…

ctfshow--xss靶场web327-web333(一命速通不了的靶场)

前言 欢迎来到我的博客 个人主页:北岭敲键盘的荒漠猫-CSDN博客 web327 打开页面是一个写信页面。 这里题目暗示不明显。 这里是要给admin写信&#xff0c;让他触发我们的xss。(不看解析不知道有个admin用户) payload: <svg οnlοadwindow.location.hrefhttp://xss平台地址…

法律文件智能识别:免费OCR平台优化数字化管理

一、系统概述 在法律行业&#xff0c;纸质文件的数字化需求日益迫切&#xff0c;合同、判决书、协议等文件的管理成为法律部门的一大难题。传统手动输入不仅耗时&#xff0c;且易出错。思通数科的OCR识别平台应运而生&#xff0c;以其开源、免费的特性为法律文档管理提供了智能…

Flutter-Engine 的定制实践:Text 绘制流程浅析及自定义underline的间距

前言 最近工作中处理的文本相关的内容较多&#xff0c;不论是刁钻的需求还是复杂的问题&#xff0c;最终都会引向一点“Flutter中的文本是如何绘制的&#xff1f;”。 这里我将以“调整下划线与文字的间距”为切入点并结合自定义Engine&#xff0c;记录一下我的个人分析和实践…

“基金申请精要:国自然基金撰写与SCI发表“

国自然基金项目撰写技巧与ChatGPT融合应用 随着社会经济发展和科技进步&#xff0c;基金项目对创新性的要求越来越高。国家级和省级等各类项目的申请层出不穷&#xff0c;项目书的撰写几乎占据了申请人的全年时间。申请人既需要提出独特且有前瞻性的研究问题&#xff0c;具备突…

java的依赖注入

java的依赖注入是个什么东西&#xff1f; 计算机专业相关知识2024-08-07 17:26河北 摘要 •帮你速读文章内容 java中的依赖注入&#xff08;Dependency Injection, DI&#xff09;是一种软件设计模式&#xff0c;用于减少代码间的耦合度。它允许一个对象&#xff08;被依赖的…

别再为质量问题头疼了,六西格玛黑带培训来救场!

六西格玛&#xff0c;这一源自摩托罗拉的先进质量管理理念&#xff0c;以其严谨的数据分析、持续的流程改进和追求卓越的核心价值&#xff0c;在全球范围内赢得了广泛的认可与应用。而六西格玛黑带&#xff0c;作为这一体系中的精英&#xff0c;不仅掌握了深厚的统计学知识&…

深度学习基础知识-Batch Normalization(BN)超详细解析

一、背景和问题定义 在深层神经网络&#xff08;Deep Neural Networks, DNNs&#xff09;中&#xff0c;层与层之间的输入分布会随着参数更新不断发生变化&#xff0c;这种现象被称为内部协变量偏移&#xff08;Internal Covariate Shift&#xff09;。具体来说&#xff0c;由…

优雅的LUA数据记录方法-serpent序列化+LUA Table

目录 简述如何集成&#xff1f;如何使用序列化 反序列化 参考 简述 项目里需要使用LUA脚本将数据记录到文件&#xff0c;要方便的增加、查找、删除&#xff0c;要方便的加载与存回。 使用序列化/反序列化 lua table可以很容易实现这些功能。 序列化将table转换为字符串 反序列…

2024双11海外购物新选择,逆向代购商家的营销利器

各大电商平台早已蓄势待发&#xff0c;迎接双11这场年度消费盛宴。与往年相比&#xff0c;今年的双11筹备工作启动得更早&#xff0c;国庆假期刚一结束&#xff0c;各大平台便进入了紧张的筹备阶段。对于遍布全球的海外购物者而言&#xff0c;无论是热衷于探索中国文化的外国人…

Cyber​​Panel upgrademysqlstatus 远程命令执行漏洞(QVD-2024-44346)

0x01 产品简介 CyberPanel是一个开源的Web控制面板,它提供了一个用户友好的界面,用于管理网站、电子邮件、数据库、FTP账户等。CyberPanel旨在简化网站管理任务,使非技术用户也能轻松管理自己的在线资源。 0x02 漏洞概述 该漏洞源于upgrademysqlstatus接口未做身份验证和…

【万户软件-注册安全分析报告-无验证方式导致安全隐患】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 1. 暴力破解密码&#xff0c;造成用户信息泄露 2. 短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉 3. 带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造…

大舍传媒:海外发稿传统新闻媒体发布新闻稿与门户媒体宣发的区别?

大舍传媒&#xff1a;海外发稿传统新闻媒体发布新闻稿与门户媒体宣发的区别&#xff1f; 在当今全球化的信息时代&#xff0c;新闻报道的传播渠道日益多元化&#xff0c;企业和个人在进行海外发稿时&#xff0c;往往面临着在传统新闻媒体和门户媒体之间的选择。那么&#xff0…

【在Linux世界中追寻伟大的One Piece】Socket编程TCP

目录 1 -> TCP socket API 2 -> V1 -Echo Server 2.1 -> 测试多个连接的情况 1 -> TCP socket API socket()&#xff1a; socket()打开一个网络通讯端口&#xff0c;如果成功的话&#xff0c;就像open()一样返回一个文件描述符。应用程序可以像读写文件一样用r…

NCCL安装(Ubuntu等)

目录 一、NCCL的定义二、安装NCCL的原因1、加速多GPU通信2、支持流行的深度学习框架3、提高计算效率4、易于使用和集成5、可扩展性 三、NCCL安装方法1、下载安装包2、更新APT数据库3、使用APT安装libnccl2包&#xff0c;另外&#xff0c;如果需要使用NCCL编译应用程序&#xff…