Di sini saya mencoba dengan login pengguna khusus di djangorestfulapi tetapi saya mendapatkan kesalahan bahwa "detail": "Metode \"POST\" tidak diizinkan." . adakah yang bisa menjelaskan di mana saya salah?

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 titik akhir:

path('api/login/',LoginAPIView.as_view(), name='login'),
0
Psynora 4 Maret 2020, 20:14

1 menjawab

Jawaban Terbaik

Dalam tampilan berbasis kelas, Anda harus mendefinisikan metode terpisah untuk metode http terpisah seperti ini

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")
1
Muhammad Hassan 4 Maret 2020, 18:09