drf基本使用

1、安装djangorestframework

pip install djangorestframework -i https://pypi.doubanio.com/simple

2、创建一个drf01的应用,来使用 drf ;  并在一个django工程settings.py中, 应用里面注册 rest_framework应用 、drf01应用

3、编写drf01/views.py里的视图

from django.shortcuts import render, HttpResponse
from django.views import  View
from django.http import JsonResponse

class Users(View):
    def get(self, request, *args, **kwargs):
        return JsonResponse(
                {
                    "status": "200",
                    "message":" django get ok",
                    "results" : []
                }
            )
    def post(self,request,  *args, **kwargs):
        return JsonResponse(
                {
                    "status": "200",
                    "message":" django post ok",
                    "results" : []
                }
            )

    def put(self,request,  *args, **kwargs):
        print( request.FILES)
        print(request.POST)
        print( request.GET)
        print(request.body)
        return JsonResponse(
                {
                    "status": "200",
                    "message":" django put ok",
                    "results" : []
                }
            )


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

class Teachers(APIView):

    def get(self, request, *args, **kwargs):
        print(request.GET)
        return Response(
            {
                "status": "200",
                "message": " django restframework get ok",
                "results": []
            }
        )

    def post(self, request, *args, **kwargs):
        print(request.data)
        return Response(
            {
                "status": "200",
                "message": " django restframework post ok",
                "results": []
            }
        )

    def put(self, request, *args, **kwargs):
        print(request.data)
        return Response(
            {
                "status": "200",
                "message": " django restframework put ok",
                "results": []
            }
        )

    def patch(self, request, *args, **kwargs):
        print(request.data)
        return Response(
            {
                "status": "200",
                "message": " django restframework patch ok",
                "results": []
            }
        )

    def delete(self, request, *args, **kwargs):
        print(request.data)
        return Response(
            {
                "status": "200",
                "message": " django restframework delete ok",
                "results": []
            }
        )

4、注释掉crsf中间件; django自带的请求中,有csrf_token来做跨站请求防攻击;所以,测试django原生的post/put/patch/delete方法,需要注释掉csrf中间件;

但是 drf中as_view()调用了django中原生的as_view()方法, 但是局部禁用了csrf;

5、在drf01应用下urls.py里面配置路由

6、在工程下配置路由分发,将drf01请求分发到drf01路由下进行处理;

7、django中,CBV原生的五种请求方法,   get请求参数中在request.get ;  put请求参数在 request.body中

 

8、使用postman方式请求的使用,“teachers/”最后的斜杠要带上, 因为在浏览器中输入如果不带斜杠, django或者drf都会自动给带上,但是在postman里面不会,只能自己手动带上;

 

猜你喜欢

转载自www.cnblogs.com/harryTree/p/11965631.html
DRF