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

相关推荐
司徒轩宇20 分钟前
Python secrets模块:安全随机数生成的最佳实践
运维·python·安全
用户785127814701 小时前
源代码接入 1688 接口的详细指南
python
vortex51 小时前
Python包管理与安装机制详解
linux·python·pip
辣椒http_出海辣椒2 小时前
如何使用python 抓取Google搜索数据
python
Ciel_75212 小时前
AmazeVault 核心功能分析,认证、安全和关键的功能
python·pyqt·pip
王国强20093 小时前
Python 异步编程的原理与实践
python
不枯石4 小时前
Python实现RANSAC进行点云直线、平面、曲面、圆、球体和圆柱拟合
python·计算机视觉
站大爷IP4 小时前
Python Lambda:从入门到实战的轻量级函数指南
python
深盾安全4 小时前
Python 装饰器精要
python
站大爷IP4 小时前
Python爬虫基本原理与HTTP协议详解:从入门到实践
python