1djangorestframework:django的app,只能再django上使用 -djangorestframework是django的一个app,更快速在django框架上的写接口 2pip3 install djangorestframework 3简单使用,看代码 django: 2.0.7,1版本也可以 djangorestframework:3.12.1 4在settings.py中注册(不注册也可以,但不规范) INSTALLED_APPS =[ 。。。 'rest_framework', ]
settings.py
INSTALLED_APPS =[ 。。。 'rest_framework', ]
urls.py
from django.contrib importadmin from django.urls importpath from app01 importviews urlpatterns =[ path('admin/', admin.site.urls), path('', views.Index.as_view()), ]
views.py
from rest_framework.views importAPIView from rest_framework.response importResponse classIndex(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'})
运行显示的页面