"detail": "Метод \" POST \ "не разрешен." - PullRequest
0 голосов
/ 04 марта 2020

здесь я попытался с пользовательским логином в djangorestfulapi, но я получаю сообщение об ошибке "detail": "Метод \" POST \ "не разрешен". , Кто-нибудь может объяснить, где я ошибаюсь?

class LoginAPIView(APIView):
    def user_login(self,request,format=None):
        # context = RequestContext(request)
        if request.method == 'POST':
            user = ''' SELECT * FROM users '''
            # Gather the username and password provided by the user.

            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            print("auth",str(authenticate(username=username, password=password)))

            if user:
                # Is the account active? It could have been disabled.
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect('/')
            else:
                return HttpResponse("xxx")
        else:
            # Bad login details were provided. So we can't log the user in.
            print ("Invalid login details: {0}, {1}".format(username, password))
            return HttpResponse("Invalid login details supplied.")

URL-адрес конечной точки:

path('api/login/',LoginAPIView.as_view(), name='login'),

1 Ответ

1 голос
/ 04 марта 2020

В представлениях, основанных на классе, вы должны определить отдельный метод для отдельных методов http, например:

class LoginAPIView(APIView):
    def post(self,request,format=None):
        user = ''' SELECT * FROM users '''
            # Gather the username and password provided by the user.

        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        print("auth",str(authenticate(username=username, password=password)))

        if user:
            # Is the account active? It could have been disabled.
            if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse("xxx")
...