12 2023 档案

摘要:自定义全局异常处理 drf异常处理交给exception_handler处理了,但是没处理非drf的异常 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler' 我们可以重写一个exception_handler方法,处理drf异常 阅读全文
posted @ 2023-12-28 17:00 wellplayed 阅读(85) 评论(0) 推荐(0) 编辑
摘要:分页组件的使用 有三种分页方式 需要新建一个py文件,以pagination.py为例 方式一:基本分页 第一步:导入分页类 from rest_framework.pagination import PageNumberPagination 第二步:书写分页类,继承 PageNumberPagin 阅读全文
posted @ 2023-12-28 16:37 wellplayed 阅读(33) 评论(0) 推荐(0) 编辑
摘要:过滤组件的使用 有三种使用方式 方式一:使用drf内置过滤类 第一步:保证视图类继承 GenericAPIView 及其子类 # 以图书类为例 class BookView(ViewSetMixin, ListAPIView): queryset = Book.objects.all() seria 阅读全文
posted @ 2023-12-28 15:50 wellplayed 阅读(33) 评论(0) 推荐(0) 编辑
摘要:排序组件快速使用 第一步:视图类需继承GenericAPIView或其子类 # 以图书类为例 class BookView(ViewSetMixin, ListAPIView): queryset = Book.objects.all() serializer_class = BookSeriali 阅读全文
posted @ 2023-12-28 15:27 wellplayed 阅读(33) 评论(0) 推荐(0) 编辑
摘要:频率组件的书写 例:书写频率组件:一分钟只能访问5次(CommonThrottle) 第一步:新建一个py文件(以throttling为例) 第二步:书写频率类,并继承继承SimpleRateThrottle # 首先导入模块 from rest_framework.throttling impor 阅读全文
posted @ 2023-12-27 15:30 wellplayed 阅读(15) 评论(0) 推荐(0) 编辑
摘要:权限组件的书写 例:书写权限组件(CommonPermission) 第一步:新建一个py文件(以permission为例) 第二步:书写认证类,并继承继承BasePermission # 首先导入模块 from rest_framework.permissions import BasePermi 阅读全文
posted @ 2023-12-27 15:19 wellplayed 阅读(10) 评论(0) 推荐(0) 编辑
摘要:第一步:进入settings 第二步:自定义或改写默认代码 阅读全文
posted @ 2023-12-26 20:43 wellplayed 阅读(60) 评论(0) 推荐(0) 编辑
摘要:认证组件的书写 例:书写登录认证(LoginAuth) 第一步:新建一个py文件(以auth为例) 第二步:书写认证类,并继承BaseAuthentication # 首先导入模块 from rest_framework.authentication import BaseAuthenticatio 阅读全文
posted @ 2023-12-26 16:25 wellplayed 阅读(7) 评论(0) 推荐(0) 编辑
摘要:自动生成路由 第一步:导入 from rest_framework.routers import SimpleRouter, DefaultRouter 第二步:实例化 router = SimpleRouter() 第三步:注册路径(以BookView为例) router.register('bo 阅读全文
posted @ 2023-12-26 16:12 wellplayed 阅读(10) 评论(0) 推荐(0) 编辑
摘要:方案一:在表模型(models)中写,在序列化类中映射 模型层书写: def publish_detail(self): return {'name': self.publish.name, 'city': self.publish.city} 序列化类(serializer)中书写: publis 阅读全文
posted @ 2023-12-21 14:58 wellplayed 阅读(39) 评论(0) 推荐(0) 编辑
摘要:1.分析APIVIew时,我们可以了解——以后的request都是drf提供的Request的对象了 from rest_framework.request import Request 2.源码分析 生成新request的部分源码: # 先看 __init__:类实例化得到对象时,对对象进行初始化 阅读全文
posted @ 2023-12-20 15:25 wellplayed 阅读(14) 评论(0) 推荐(0) 编辑
摘要:1.和CBV源码执行流程相似,请求来了先走路由层: path('books/', views.BookView.as_view()) 2.走APIView的as_view方法,代码如下: @classmethod def as_view(cls, **initkwargs): view = supe 阅读全文
posted @ 2023-12-20 15:08 wellplayed 阅读(12) 评论(0) 推荐(0) 编辑
摘要:问题详细: 在使用KindEditor上传图片时,后端已经接收到了,但前端仍在加载 如下图: 解决办法: 方法一: 在配置文件中书写以下代码: X_FRAME_OPTIONS = 'ALLOWALL' 方法二: 注释中间件: 'django.middleware.clickjacking.XFram 阅读全文
posted @ 2023-12-13 19:05 wellplayed 阅读(215) 评论(1) 推荐(1) 编辑
摘要:forms组件的作用 1 渲染页面 2 校验数据 3 渲染错误 forms组件的使用方法 书写: 首先需要新建一个py文件,并导入模块 from django import forms 再书写一个类继承forms.Form class MyRegForm(forms.Form): 书写字段: use 阅读全文
posted @ 2023-12-08 08:56 wellplayed 阅读(9) 评论(0) 推荐(0) 编辑
摘要:安装 captcha 库 pip install captcha 基本使用方法(生成图片验证码) import captcha from captcha.image import ImageCaptcha # 设置图片宽高 image = ImageCaptcha(width=200, height 阅读全文
posted @ 2023-12-07 16:29 wellplayed 阅读(828) 评论(0) 推荐(0) 编辑
摘要:在python项目中,需要有一个txt文件requirements.txt,里面面书写当前项目中所有的依赖 书写规范如下: requirements.txt 内 django==3.2.20 pillow mysqlclient 以后使用项目时输入以下命令: pip install -r requi 阅读全文
posted @ 2023-12-07 14:46 wellplayed 阅读(119) 评论(0) 推荐(0) 编辑
摘要:数据库主要有五种连表方式 首先需要准备两张表: 图书表 book id name price publish_id 1 西游记 33 1 2 红楼梦 56 1 3 三国演义 66 2 4 西厢记 55 6 出版社表 publish id name addr 1 北京出版社 北京 2 南京出版社 南京 阅读全文
posted @ 2023-12-05 15:01 wellplayed 阅读(373) 评论(0) 推荐(0) 编辑
摘要:安装 pip install loguru 1、输出日志 from loguru import logger logger.debug("这是一条debug日志") 终端执行后出现带颜色的日志: 2、输出到文件 from loguru import logger logger.add("file_{ 阅读全文
posted @ 2023-12-01 17:29 wellplayed 阅读(129) 评论(0) 推荐(0) 编辑
摘要:扩展auth_user表 方法一: from django.contrib.auth.models import User # 第一种: 建立一对一外键关系 (不推荐) class UserDetail(models.Model): phone = models.BigIntegerField() 阅读全文
posted @ 2023-12-01 15:40 wellplayed 阅读(11) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示