[Answered ]-How to fix 500 internal server error while using Django and React

1👍

You have this error because the user you try to assign is not authenticated.
Add a permission to your view like this :

from rest_framework import permissions

class add_to_cart_view(APIView):
    permission_classes = [permissions.IsAuthenticated]
    
    # Rest of the code here

NB : You will need to authenticate the user before make a post request.

UPDATE : If you want to test with non authenticated user.
You can create a generic user for that :

# Create a user with a name unauthenticated_user in the admin

# In the view :
if request.user.is_authenticated:
    # Do something for authenticated users.
    # Your code previous code here
else:
    # retrieve the unauthenticated_user you create 
    # use your custom user here

Leave a comment