码迷,mamicode.com
首页 > 其他好文 > 详细

视图组件和认证组件

时间:2019-03-27 21:27:22      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:get   lse   publish   class   ram   组件   create   code   request对象   

1.使用mixin和generics类编写视图 

from
rest_framework.mixins import CreateModelMixin, ListModelMixin, DestroyModelMixin, RetrieveModelMixin, UpdateModelMixin from rest_framework.generics import GenericAPIView class BooksView(CreateModelMixin, ListModelMixin, GenericAPIView): serializer_class = BookSerializer queryset = models.Book.objects.all() def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class BookView(UpdateModelMixin, DestroyModelMixin, RetrieveModelMixin, GenericAPIView): serializer_class = BookSerializer queryset = models.Book.objects.all() def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs)

2.使用generics 下ListCreateAPIView,RetrieveUpdateDestroyAPIView 编写视图

from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView


class BooksView(ListCreateAPIView):
    queryset = models.Book.objects.all()
    serializer_class = BookSerializer


class BookView(RetrieveUpdateDestroyAPIView):
    queryset = models.Book.objects.all()
    serializer_class = BookSerializer

3. 使用ModelViewSet 编写视图

url配置:
url(r^books/(?P<pk>\d+), views.BooksView.as_view({get: retrieve, put: update, delete: destroy})),
url(r^books/$, views.BooksView.as_view({get: list, post: create})),
views层:
# 使用ModelViewSet
from rest_framework.viewsets import ModelViewSet

class BooksView(ModelViewSet):
    queryset = models.Book.objects.all()
    serializer_class = BookSerializer

4.ViewSetMixin的使用

url 配置
 url(r^test/, views.Publish.as_view({
        get: get_all, post: create_one, put: update, delete: delete
    })),

views:
from rest_framework.viewsets import ViewSetMixin


class Publish(ViewSetMixin, APIView):
    def get_all(self, request):
        return HttpResponse(get_all)

    def create_one(self, request):
        return HttpResponse(create_one)

    def update(self, request):
        return HttpResponse(update)

    def delete(self, request):
        return HttpResponse(delete)

5. 认证组件

drf认证组件源码实现
‘‘‘
1: APIView的dispach方法 2.self.initial(request, *args, **kwargs)方法 3. self.perform_authentication(request)[认证] 4.def perform_authentication(self, request) 5. request.user-> Request(request为dispach中替换过的新的request) 6.Request中找到user 7.self._authenticate()[self是新的request对象] 8.Request类的_authenticate(self)方法,深度 ‘‘‘
from rest_framework.views import APIView
from rest_framework.exceptions import AuthenticationFailed
import uuid
from rest_framework.request import Request


class CheckLogin():
    def authenticate(self, request):
        token = request.GET.get(token)
        ret = models.UserToken.objects.filter(token=token).first()
        if ret:
            return None
        else:
            raise AuthenticationFailed(验证失败)

    def authenticate_header(self, request):
        pass


class BooksView(APIView):
    authentication_classes = [CheckLogin]

    def get(self, request):
        return HttpResponse(ok)


class Login(APIView):
    def post(self, request):
        response = {
            status: 100,
            msg: None
        }
        name = request.data.get(name)
        pwd = request.data.get(pwd)
        # 数据库校验该用户是否存在
        user = models.User.objects.filter(name=name, pwd=pwd).first()
        if user:
            # 正常返回登录成功, 返回唯一token
            token = uuid.uuid4()
            ret = models.UserToken.objects.update_or_create(user=user, defaults={token: token})
            response[msg] = 登录成功
            response[token] = token
        else:
            response[msg] = 登录失败
            response[status] = 101
        return Response(response)

 

视图组件和认证组件

标签:get   lse   publish   class   ram   组件   create   code   request对象   

原文地址:https://www.cnblogs.com/yangyufeng/p/10610523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!