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

相关推荐
Csvn1 天前
🌟 LangChain 30 天保姆级教程 · Day 13|OutputParser 进阶!让 AI 输出自动转为结构化对象,并支持自动重试!
python·langchain
cch89181 天前
Python主流框架全解析
开发语言·python
sg_knight1 天前
设计模式实战:状态模式(State)
python·ui·设计模式·状态模式·state
好运的阿财1 天前
process 工具与子agent管理机制详解
网络·人工智能·python·程序人生·ai编程
张張4081 天前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_423533991 天前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
小白学大数据1 天前
Selenium+Python 爬虫:动态加载头条问答爬取
爬虫·python·selenium
Hui Baby1 天前
springboot读取配置文件
后端·python·flask
阿Y加油吧1 天前
回溯法经典难题:N 皇后问题 深度解析 + 二分查找入门(搜索插入位置)
开发语言·python