1、初识drf

drf的学习需要学习者有django基本使用知识。

文章目录

什么是drf,有什么作用

  • drf(django rest-framework),让使用者更快捷的开发规范化的接口。
  • 提供了权限、身份验证、限流等实用功能。
  • 基于django CBV来实现的

CBV是什么

FBV是什么?

函数视图

python 复制代码
def index(request):
    return HttpResponse("123")

CBV是什么?

类视图

python 复制代码
class IndexView(View):

    def get(self, request):
        return HttpResponse("view get")

函数是代码的封装、类是函数的封装

初步使用

drf 下载以及django创建项目

shell 复制代码
pip install djangorestframework

# 创建一个django项目
django-admin startproject drf1

# 创建一个app
python manage.py startapp fbv

drf已经不需要,在setting的INSTALLED_APPS里面注册了。

django最小启动内容

修改setting

我们已将django内置的所有app、中间件、模版全部停用如下。这样会导致drf报错我们需要加如下代码

python 复制代码
# 解决drf报错
REST_FRAMEWORK = {
    "UNAUTHENTICATED_USER": None,
}

# 停用所有apps
INSTALLED_APPS = [
    # 'django.contrib.admin',
    # 'django.contrib.auth',
    # 'django.contrib.contenttypes',
    # 'django.contrib.sessions',
    # 'django.contrib.messages',
    # 'django.contrib.staticfiles',
    'fbv',	# 注册我们自己的app
]

# 停用所有内置中间件
MIDDLEWARE = [
    # 'django.middleware.security.SecurityMiddleware',
    # 'django.contrib.sessions.middleware.SessionMiddleware',   # session管理
    # 'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',  # CSRF验证机制、一般用于前后端不分离
    # 'django.contrib.auth.middleware.AuthenticationMiddleware',    # 身份验证中间件
    # 'django.contrib.messages.middleware.MessageMiddleware',      # 消息中间件
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# 模版禁用
TEMPLATES = [
    #     {
    #         'BACKEND': 'django.template.backends.django.DjangoTemplates',
    #         'DIRS': [],
    #         'APP_DIRS': True,
    #         'OPTIONS': {
    #             'context_processors': [
    #                 'django.template.context_processors.debug',
    #                 'django.template.context_processors.request',
    #                 'django.contrib.auth.context_processors.auth',
    #                 'django.contrib.messages.context_processors.messages',
    #             ],
    #         },
    #     },
]
修改 url
python 复制代码
# from django.contrib import admin
from django.urls import path

urlpatterns = [
    # path('admin/', admin.site.urls),
]

编写drf视图

python 复制代码
from rest_framework.response import Response
from rest_framework.views import APIView


class RFWView(APIView):
    def get(self, request):
        return Response("drf view get")

编辑url

python 复制代码
from fbv.views import index, IndexView, RFWView

urlpatterns = [
    path('RFWView/', RFWView.as_view()),
]

测试返回结果

postman中请求 [http://127.0.0.1:8000/RFWView/](http://127.0.0.1:8000/RFWView/)地址返回结果为 drf view get

相关推荐
Doker 多克20 分钟前
Django 缓存框架
python·缓存·django
miracletiger2 小时前
uv 新的包管理工具总结
linux·人工智能·python
我不会编程5552 小时前
Python Cookbook-6.10 保留对被绑定方法的引用且支持垃圾回收
开发语言·python
ʚɞ 短腿欧尼2 小时前
关系数据的可视化
python·pycharm·可视化·数据可视化·图表
noravinsc4 小时前
django admin 中更新表数据 之后再将数据返回管理界面
数据库·django·sqlite
Bruce-li__5 小时前
DRF凭什么更高效?Django原生API与DRF框架开发对比解析
数据库·django·sqlite
PXM的算法星球5 小时前
【软件工程】面向对象编程(OOP)概念详解
java·python·软件工程
Humbunklung6 小时前
PySide6 GUI 学习笔记——常用类及控件使用方法(常用类矩阵QRectF)
笔记·python·学习·pyqt
noravinsc6 小时前
connection.cursor() 与 models.objects.filter
数据库·django·原生查询·orm查询
蹦蹦跳跳真可爱5896 小时前
Python----深度学习(基于DNN的吃鸡预测)
python·深度学习·dnn