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

相关推荐
Q一件事3 分钟前
结构方程相关
python·算法·机器学习
SunnyDays101114 分钟前
使用 Python 轻松操控 Excel 网格线:隐藏、显示与自定义颜色
开发语言·python·excel
BubbleCodes16 分钟前
使用Conda和pip创建Python环境
python·conda·pip
我不听你讲话23 分钟前
Nginx核心功能
linux·服务器·python
计算机徐师兄27 分钟前
Python基于数字水印的图像版权保护系统(附源码,文档说明)
python·数字水印·图像版权保护系统·python图像版权保护系统·python数字水印图像版权·python数字水印·数字水印图像版权保护系统
小陳参上33 分钟前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
李昊哲小课34 分钟前
Python 线性数据结构详解
开发语言·数据结构·python
新诺韦尔API36 分钟前
身份证验证接口详细开发对接指南
大数据·python·api
WangYaolove131436 分钟前
基于python的多媒体资料管理系统(源码+文档)
python·mysql·django·毕业设计·源码
李昊哲小课42 分钟前
Python 数据结构示例
开发语言·数据结构·python