[Answered ]-Checking the type of an argument in Django Python views

1👍

Seems to be working now.

But a few things I have since realised. EmailField is probably not even a valid Python type to begin with!

Secondly, I have corrected the type checking (which was probably wrong in the first place).

Lastly, modified the url.py to look like this now:

urls.py

path('<int:arg>/', views.getUpdateDeleteAccount),
path('<str:arg>/', views.getUpdateDeleteAccount),

views.py

def getUpdateDeleteAccount(request, arg):
    """To get, update and delete a single user account"""

    if type(arg) is int:
        try:
            account = Account.objects.get(id=arg)
        except Account.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
    elif type(arg) is str:
        try:
            account = Account.objects.get(emailAddress=arg)
        except Account.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)
👤Sadiq

Leave a comment