drf—— drf的安装和使用

一、drf的安装

1 djangorestframework:django的app,只能再django上使用
        -djangorestframework是django的一个app,更快速在django框架上的写接口
2 pip3 install djangorestframework  
3 简单使用,看代码
    django: 2.0.7 ,1版本也可以
    djangorestframework:3.12.1
4 在settings.py中注册(不注册也可以,但不规范)
INSTALLED_APPS = [
    。。。
    'rest_framework',
]

二、drf的简单使用

settings.py

INSTALLED_APPS = [
    。。。
    'rest_framework',
]

urls.py

from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.Index.as_view()),

]

views.py

from rest_framework.views import APIView
from rest_framework.response import Response

class Index(APIView):
    def get(self, request, *args, **kwargs):
    '''
# 这个request是新的drf的Request类的对象 print(type(request)) print(type(request._request)) print(request._request.method) # 这个没有疑问 print(request.method) # 为什么能打印出来 print(request.POST) # 跟JsonResponse很像 print(request.data)
    '''
return Response({'name': 'lili'})

 运行显示的页面

 

posted @ 2020-11-04 19:57  1024bits  阅读(138)  评论(0编辑  收藏  举报