lundi 31 août 2020

How to DJANGO Web Token and Save Procces (POST)?

I am trying to add data on views.py, I send username and password parameters on JSON body with POST method from POSTMAN . Normally The function work succesfully like below:

json_str=((request.body).decode('utf-8'))
    json_data = json.loads(json_str)
    new_device = Device(id=json_data["id"], status=json_data["status"])
    try:
        new_device.save()
        resp_data = {
        'code': 200,
        "success": True
    }
        return JsonResponse(resp_data)
    except Exception as e:
        error_data = {
            "success": False,
            "error": str(e)
            }

        return JsonResponse(error_data)

After enable djangorestframework_simplejwt package on my project and run it successfully I changed urls.py link like: path('add/', add_device) to path('add/', add_device.as_view()) On views.py:

class add_device(APIView):
    permission_classes = (IsAuthenticated,)
    
    def get(self,request):
        
        json_str=((request.body).decode('utf-8'))
        json_data = json.loads(json_str)
        new_device = Device(id=json_data["id"], status=json_data["status"])
        try:
            new_device.save()
            resp_data = {
            'code': 200,
            "success": True
        }
            return JsonResponse(resp_data)
        except Exception as e:
            error_data = {
                "success": False,
                "error": str(e)
                }

            return JsonResponse(error_data)

But now, always a json error message returns to me:

{
    "detail": "Method \"POST\" not allowed."
}

How do I fix it ?

Thank for all helps..




Aucun commentaire:

Enregistrer un commentaire