The code I'm showing you below its what works for me right now. its not the most secure but does the job but i want to do it using POST method. any ideas how to change it?
I have a serializer.py class
class userLoginSerializer(serializers.ModelSerializer):
class Meta:
model = users
fields = ('nick', 'pass_field')
@api_view(['GET'])
def user_login(request,nick,pass_field):
but when i sent the 2 values nick and passfield it says that the nick already exist and returns 404 because it passes it to serializers.errors. I just need to pass the code using POST and validating if it exist and return a success JSON. The code below works but its not the best implementation.
if request.method == 'GET':
try:
users.objects.get(nick=nick,pass_field=pass_field)
json = {}
json['message'] = 'success'
return Response(json, status=status.HTTP_201_CREATED)
except users.DoesNotExist:
json = {}
json['message'] = 'error'
return Response(json, status=status.HTTP_400_BAD_REQUEST)
POSTrequest with username and password looks very much as authentication. Please tell us more about why do you do the request at the first place. Maybe there's a better way to solve your problem.