标签:size .com session obj model org guid basic pass
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查看
标签:size .com session obj model org guid basic pass
原文地址:https://www.cnblogs.com/chenyishi/p/10658468.html