前言django中編輯視圖views.py有兩種方式,一種是基于類的實(shí)現(xiàn),另外一種是函數(shù)式的實(shí)現(xiàn)方式,兩種方法都可以用。 REST框架提供了一個(gè)APIView類,它是Django View類的子類。 View與APIView的區(qū)別View 是Django默認(rèn)的視圖基類,APIView 是REST framework提供的所有視圖的基類, 繼承自Django的View 。
APIView 與View 的不同之處在于:
傳入到視圖方法中的是REST framework的Request對(duì)象,而不是Django的HttpRequeset對(duì)象; 視圖方法可以返回REST framework的Response對(duì)象,視圖會(huì)為響應(yīng)數(shù)據(jù)設(shè)置(render)符合前端要求的格式; 任何APIException異常都會(huì)被捕獲到,并且處理成合適的響應(yīng)信息; APIException異常捕獲 在進(jìn)行dispatch()分發(fā)前,會(huì)對(duì)請(qǐng)求進(jìn)行身份認(rèn)證、權(quán)限檢查、流量控制。
django的View部分源碼 class View: """ Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking. """
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
def __init__(self, **kwargs): """ Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things. """ # Go through keyword arguments, and either save their values to our # instance, or raise an error. for key, value in kwargs.items(): setattr(self, key, value) REST framework的APIView 繼承了django的View ,部分源碼如下 class APIView(View):
# The following policies may be set at either globally, or per-view. renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES parser_classes = api_settings.DEFAULT_PARSER_CLASSES authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES throttle_classes = api_settings.DEFAULT_THROTTLE_CLASSES permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES content_negotiation_class = api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS metadata_class = api_settings.DEFAULT_METADATA_CLASS versioning_class = api_settings.DEFAULT_VERSIONING_CLASS
# Allow dependency injection of other settings to make testing easier. settings = api_settings
schema = DefaultSchema() APIView 多了一些屬性和方法,比如:身份認(rèn)證、權(quán)限檢查、流量控制
django的View先使用django自帶的view,獲取一個(gè)Card表里面的卡號(hào)信息: models.py設(shè)計(jì)card表 # models.py class Card(models.Model): '''銀行卡 基本信息 # 作者:上海悠悠,QQ交流群:750815713''' card_id = models.CharField(max_length=30, verbose_name="卡號(hào)", default="") card_user = models.CharField(max_length=10, verbose_name="姓名", default="") add_time = models.DateField(auto_now=True, verbose_name="添加時(shí)間")
class Meta: verbose_name_plural = '銀行卡賬戶' verbose_name = "銀行卡賬戶_基本信息"
def __str__(self): return self.card_id views.py視圖的編寫(xiě) from django.http import JsonResponse from rest_framework import serializers from django.core import serializers as dj_serializers # 避免和rest_framework里面的serializers沖突 from .models import * from django.views.generic.base import View import json # 作者:上海悠悠,QQ交流群:750815713
class CardListView(View): '''基于django的view實(shí)現(xiàn)獲取card列表''' def get(self, request): data = {} cards = Card.objects.all() data['result'] = json.loads(dj_serializers.serialize("json", cards)) return JsonResponse(data) urls.py設(shè)置訪問(wèn)地址 from apiapp import views from django.conf.urls import url # 作者:上海悠悠,QQ交流群:750815713
urlpatterns = [ url(r'^api/v1/cards/$', views.CardListView.as_view()),
] 訪問(wèn)http://127.0.0.1:8000/api/v1/cards/ ,測(cè)試結(jié)果 REST framework的APIViewREST framework的APIView 繼承了django的View 類,先序列化Card類,這里的序列化用rest_framework 里面的ModelSerializer from rest_framework.response import Response from rest_framework.views import APIView from rest_framework import serializers from .models import * from rest_framework.permissions import IsAuthenticated,AllowAny from rest_framework.authentication import TokenAuthentication # 作者:上海悠悠,QQ交流群:750815713
class CardAPISerializer(serializers.ModelSerializer): # 繼承自ModelSerializer類 '''序列化數(shù)據(jù)的類,根據(jù)model表來(lái)獲取字段''' class Meta: model = Card fields = '__all__'
class CardListAPIView(APIView): '''REST framework的APIView實(shí)現(xiàn)獲取card列表 # 作者:上海悠悠,QQ交流群:750815713''' # authentication_classes = (TokenAuthentication,) # token認(rèn)證 # permission_classes = (IsAuthenticated,) # IsAuthenticated 僅通過(guò)認(rèn)證的用戶 permission_classes = (AllowAny,) # 允許所有用戶
def get(self, request, format=None): """ Return a list of all users. """ cards = Card.objects.all() serializer = CardAPISerializer(cards, many=True) return Response(serializer.data) 配置urls.py,設(shè)置訪問(wèn)地址 from apiapp import views from django.conf.urls import url # 作者:上海悠悠,QQ交流群:750815713
urlpatterns = [ url(r'^api/v1/cardlist/$', views.CardListAPIView.as_view()),
] 訪問(wèn)http://127.0.0.1:8000/api/v1/cardlist/ ,測(cè)試結(jié)果 2019第一期《python測(cè)試開(kāi)發(fā)》課程,10月13號(hào)開(kāi)學(xué)!本期上課時(shí)間:10月13號(hào)-12月8號(hào),每周六、周日晚上20:30-22:30
|