Foreword
django views.py edit view there are two ways, one is to achieve class-based, the other is functional implementation, two methods can be used.
APIView REST framework provides a class that is a subclass of class Django View.
View of difference and APIView
View
Django's default view is the base class APIView
is the base class for all views REST framework provided, inherited from the Django View
.
APIView
With View
the exception that:
- Passed to the view method is REST framework Request object, instead of Django HttpRequeset objects;
- The method may return REST framework view Response object, the view in response to the data set (the render) conform to the format required by the front end;
- Any ApiException exceptions are captured, and processed into a suitable response information; ApiException exception trap
- Performing dispatch () prior to distribution, will request authentication, authorization check, flow control.
View portion of the source django
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 of APIView
inherited django's View
, part of the source code as follows
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
More properties and methods, such as: authentication, authorization check, flow control
- authentication_classes Authentication
- permission_classes permission checks
- throttle_classes Flow Control
django's View
First use django own view, get a table inside the card number Card Information:
models.py design card table
# Models.py class Card (models.Model): '' ' bank card of basic information #: JAY, QQ exchange group: 123456789 ' '' card_id = models.CharField (= max_length 30, verbose_name = " card " , default = "" ) card_user = models.CharField(max_length=10, verbose_name="姓名", default="") add_time = models.DateField(auto_now=True, verbose_name="添加时间") class Meta: verbose_name_plural = ' bank card account ' verbose_name = " bank card account _ basic information " def __str__(self): return self.card_id
Write views.py view
from django.http Import jsonResponse from rest_framework Import serializers from django.core Import serializers AS dj_serializers # avoid conflict and rest_framework inside the serializers from .models Import * from django.views.generic.base Import View Import json # Author: Shanghai yo, QQ exchange group: 750 815 713 class CardListView (View): '' ' get a list of card Based django of view ' '' DEF GET (Self, Request): data = {} cards = Card.objects.all() data['result'] = json.loads(dj_serializers.serialize("json", cards)) return JsonResponse(data)