zoukankan      html  css  js  c++  java
  • django drf Token验证

    https://www.django-rest-framework.org/api-guide/authentication/#basicauthentication

    1.INSTALLED_APPS

    INSTALLED_APPS = (
        ...
        'rest_framework.authtoken'
    )

    2.REST_FRAMEWORK配置

    REST_FRAMEWORK = {
        # 'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
        # 'PAGE_SIZE':2,
        'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework.authentication.SessionAuthentication',
            'rest_framework.authentication.TokenAuthentication'
        )
    }

    3.migrate生成表

    4.创建一个token

    import sys
    import os
    
    pwd = os.path.dirname(os.path.realpath(__file__))
    sys.path.append(pwd)
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MxShop.settings')
    
    import django
    django.setup()
    
    from rest_framework.authtoken.models import Token
    
    from django.contrib.auth import authenticate
    user = authenticate(username='admin',password='admin')
    token = Token.objects.create(user=user)
    print(token.key)

    5.生成oken

    6.验证token

    可通过request.user查看

    PS:Settings.py中的TokenAuthorazition是全局的,我们也可以取消全局,只在徐娅授权的View中添加authorazition

    1.取消settings中配置

    REST_FRAMEWORK = {
        # 'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
        # 'PAGE_SIZE':2,
        'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework.authentication.SessionAuthentication',
            # 'rest_framework.authentication.TokenAuthentication'
        )
    }

    2.在View中配置

    class GoodsList(mixins.ListModelMixin,mixins.CreateModelMixin, viewsets.GenericViewSet):
        class GoodsPagination(PageNumberPagination):
            page_size = 2
            page_size_query_param = 'pageSize'
            page_query_param = 'p'
            max_page_size = 100
    
        queryset = Goods.objects.all()  # 不能切片后再过滤,例如:Goods.objects.all()[:10]
        serializer_class = GoodsSerializer
        pagination_class = GoodsPagination
        authentication_classes = (TokenAuthentication,)
        filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
        search_fields = ('=name',)  # 文档:https://www.django-rest-framework.org/api-guide/filtering/#searchfilter
        ordering_fields = ('name',)
        # filter_fields = ('name',) #逗号必加,缺点无法模糊查询
        filterset_class = GoodsFilter
  • 相关阅读:
    [转]人生以快乐为本
    不用iTunes也能添加音乐到iPod
    设计很有意思的U盘
    PhotoFunia 在线生成趣味图片
    [转]关于项目管理的一点杂感
    MVC视频序列和Demo的下载地址
    视频测试序列的下载地址
    fatal error LNK1104: 无法打开文件“LIBC.lib”错误
    ORACLE数据库性能优化概述
    oracle常用hint
  • 原文地址:https://www.cnblogs.com/chenyishi/p/10658468.html
Copyright © 2011-2022 走看看