405 Method Not Allowed

2,252 views
Skip to first unread message

Salima Begum

unread,
Aug 5, 2022, 3:30:42 AM8/5/22
to Django REST framework
Hi, 
I am new to Django rest framework. In my project I have written api view for phone OTP verification there If I use api_view(['POST']) method it is raised an error. 
```
@api_view(['POST'])
@permission_classes([AllowAny])
@renderer_classes([TemplateHTMLRenderer, JSONRenderer])
def phone_otp_verify(request, id):
    Qurycustomer = customer.objects.get(id=id)
    OTP = Qurycustomer.phone_OTP
    if request.method == 'POST':
        security_1 = request.POST['security_1']
        security_2 = request.POST['security_2']
        security_3 = request.POST['security_3']
        security_4 = request.POST['security_4']
        security_5 = request.POST['security_5']
        OTP_entered = str(security_1) + str(security_2) + str(security_3) + str(security_4) + str(security_5)
        OTP_en = int(OTP_entered)
        if OTP == OTP_en:
            Qurycustomer.cust_status = 1
            Qurycustomer.phone_verify = 1
            Qurycustomer.phone_OTP = 00000
            Qurycustomer.os_name = osname
            Qurycustomer.browser = webname
            Qurycustomer.ip_address = ip_address
            Qurycustomer.history.change_by_reason = "Phone number verified"
            Qurycustomer.save()
            return redirect('/home')
    return render(request, 'phone_otp_verify.html')
```
If I open this url(http://127.0.0.1:8000/phone-otp-verify/17) I am getting error "405 Method Not Allowed" From above code If I mute "@api_view(['POST'])" this line of code I am not getting any error How can I achieve this Please help me out.

Umair Ramzan

unread,
Aug 5, 2022, 6:13:35 AM8/5/22
to django-res...@googlegroups.com
By default browsers makes GET requests. So you need to use some other Clients such as postman to test your api.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/50f7d408-d795-469d-99a1-35245318f260n%40googlegroups.com.

Salima Begum

unread,
Aug 5, 2022, 7:35:14 AM8/5/22
to django-res...@googlegroups.com
Hi, Thanks for responding
I Tested in postman. I got same error.

naveen Kumar

unread,
Aug 5, 2022, 7:37:08 AM8/5/22
to django-res...@googlegroups.com

@api_view(['POST']) --> it means allow only post method

Replace above with @api_view(['GET', 'POST'])  if it need to allow get method as well




Reply all
Reply to author
Forward
0 new messages