在尝试使用Django rest_framework在Django中检索令牌身份验证时遇到了一些问题
Serializers.py文件:
#make random numbers that roughly match OPs
set.seed(42)
x <- 22 + runif(1000)
decs <- (x - floor(x))
x[decs > 0.0001 & decs < 0.0010]
#> [1] 22.00024 22.00041
Views.py文件:
from django.contrib.auth import get_user_model, authenticate
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
class AuthTokenSerializer(serializers.Serializer):
"""create the serializer class for the token"""
email = serializers.CharField()
password = serializers.CharField(
style = {'input-type': 'password'},
trim_whitespace = False
)
def validate(self, attrs):
"""validate and authenticate the user"""
email = attrs.get('email')
password = attrs.get('password')
user = authenticate(
request=self.context.get('request'),
username=email,
password = password
)
if not user:
msg=_('Unable to authenticate with provided credentials')
raise serializers.ValidationError(msg, code='authentication')
attrs['user'] = user
return attrs
尝试检索令牌时出现以下错误:
from rest_framework import generics
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings
from user.serializers import UserSerializer, AuthTokenSerializer
class CreateTokenView(ObtainAuthToken):
"""obtain a new token for user"""
serializer_class = AuthTokenSerializer
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'core',
'user',
]
我尝试使用py manage.py migration命令应用迁移,但是输出显示:要执行的操作:
ProgrammingError at /api/user/token/
(1146, "Table 'django.authtoken_token' doesn't exist")