• django-rest-framework学习之Requests and Responses--2017年4月13日


    Requests and Responses ---- REST framework的核心
    【1】Request objects
    HttpRequest 核心功能是request.data获取请求的主体 (类似request.POST )
    requests.data #适用各种数据,可以用于POST PUT和PATCH方法
    【2】Response objects
    return Response(data) #给与客户端请求的内容类型
    【3】Status codes
    REST framework 为每个状态码提供了明确清晰的标识符,例如HTTP_400_BAD_REQUEST
    【4】Wrapping(封装) API views
    APIView 类基础视图
    @api_view()函数基础视图
    【5】实施
    from rest_framework import status
    from rest_framework.decorators import api_view
    from rest_framework.response import Response
    from myLesson.models import MyLesson
    from myLesson.serializers import MyLessonSerializer
     
    @api_view(['GET','POST'])
    def MyLesson_list(request,format=None):
        """
        List all MyLesson,or create a new MyLesson   
        """
        if request.method == 'GET':
            myLesson = MyLesson.objects.all()
            serializer = MyLessonSerializer(myLesson,many=True)
            return Response(serializer.data)
     
        elif request.method == 'POST':
            serializer = MyLessonSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data,status=status.HTTP_201_CREATED)
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
     
    @api_view(['GET','PUT','DELETE'])
    def MyLesson_detail(request,pk,format=None):
        """
        Retrieve,update or delete a myLesson instance
        """
        try:
            myLesson = MyLesson.objects.get(pk=pk)
        except MyLesson.DoesNotExit:
            return Response(status=status.HTTP_404_NOT_FOUND)
     
        if request.method == 'GET':
            serializer = MyLessonSerializer(myLesson)
            return Response(serializer.data)
     
        elif request.method == 'PUT':
            serializer = MyLessonSerializer(myLesson,data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
     
        elif request.method == 'DELETE':
            myLesson.detele()
            return Response(status=status.HTTP_204_NO_CONTENT)
     
     
    #views.py
    def MyLesson_list(request, format=None):
    def MyLesson_detail(request, pk, format=None):
     
    #urls.py
    from rest_framework.urlpatterns import format_suffix_patterns
    urlpatterns = format_suffix_patterns(urlpatterns)
     
    我们可以用Accept header来控制Response的格式
     
    http http://127.0.0.1:8000/snippets/ Accept:application/json  # Request JSON
    http http://127.0.0.1:8000/snippets/ Accept:text/html         # Request HTML
     
    或者加一个格式后缀
     
    http http://127.0.0.1:8000/snippets.json  # JSON suffix
    http http://127.0.0.1:8000/snippets.api 
  • 相关阅读:
    android: 在fragment中添加actionbar, menu
    Android Context
    Android 在fragment中使用tabhost
    Android 菜单
    centos8 防火墙
    DHCP 原理
    阿里云服务器 linux下载 jdk
    java long值转成时间格式
    Window 中杀死指定端口 cmd 命令行 taskkill
    redis 安装
  • 原文地址:https://www.cnblogs.com/jingbostar/p/6705492.html
Copyright © 2020-2023  润新知