django rest-framework APIView详解

APIView 是对   

from django.views.generic.base import View

django 自带的 View 的封装,

先来看一下使用 View 的实例:

import json
from django.views.generic.base import View
from django.core import serializers
from django.http import HttpResponse,JsonResponse
from .models import Course

class CourseListView(View):
    def get(self, request):
        """
        通过django的view实现课程列表页
        """
        courses = Course.objects.all()[:10]
        json_data = serializers.serialize('json', Courses)
        json_data = json.loads(json_data)
        return JsonResponse(json_data, safe=False)


在  APIView  中继承了 View类,并自定义了许多的其它属性与方法。

你可能感兴趣的:(python3)