django休息时,Tocken身份验证失败

时间:2018-05-03 08:03:46

标签: django-rest-framework django-rest-auth

我可以创建用户模型。但即使存在具有给定凭据的用户

,身份验证也始终失败

models.py的用户创建方法为

package mycorp.myproject2

import mycorp.myproject.Test

object AnotherTest extends App {

  Test.info("hello")

}

views.py的登录/身份验证方法为

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)
在settings.py

class ObtainAuthToken(APIView):
    @csrf_exempt
    def post(self, request):
        user = authenticate(
        username=request.data['username'], 
        password=request.data['password'])

        if user:
            token, created = Token.objects.get_or_create(user=user)
            return Response({'token': token.key, 'user': UserSerializer(user).data})

        return Response('Invalid username or passwordee', status=status.HTTP_400_BAD_REQUEST)

settings.py就在这里

AUTH_USER_MODEL = 'account.User'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

此代码有什么问题?

0 个答案:

没有答案