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

相关推荐
忡黑梨3 小时前
BUUCTF_reverse_[MRCTF2020]Transform
c语言·开发语言·数据结构·python·算法·网络安全
芝士爱知识a3 小时前
2026年 AI 期权工具全维度测评与推荐榜单:AlphaGBM 领跑,量化交易新范式
大数据·人工智能·python·ai量化·alphagbm·ai期权工具·ai期权工具推荐
天远Date Lab3 小时前
天远入职背调报告API对接实战:Python构建自动化背景调查中台
大数据·网络·python·自动化
一叶萩Charles3 小时前
MCP 实战:国家统计局数据查询 Server 从开发到发布
javascript·人工智能·python·node.js
chushiyunen3 小时前
python双下划线魔术方法(特殊方法)(双下划线方法)
python
米码收割机3 小时前
【AI】OpenClaw问题排查
开发语言·数据库·c++·python
所谓伊人,在水一方3333 小时前
【Python数据科学实战之路】第10章 | 机器学习基础:从理论到实践的完整入门
开发语言·人工智能·python·机器学习·matplotlib
王夏奇3 小时前
Python-对excel文件操作-pandas库
python·excel·pandas
无风听海3 小时前
Python之TypeVar深入解析
开发语言·python·typevar
Amctwd3 小时前
【OS】操作系统的 I/O
django