title: How to fix 500 internal server error while using Django and React
body: <p>I'm trying to create a simple shopping cart with django and react. When I click on the add-to-cart button I get <code>500 Internal Server Error</code> from react. The error it's displaying in django is:
  <code>ValueError: Cannot assign &quot;&lt;django.contrib.auth.models.AnonymousUser object at 0x00000268F76AEFA0&gt;&quot;: &quot;order_product.user&quot; must be a &quot;User&quot; instance.</code> I have tried every means I know but I'm still not able to fix it. Below is the programme:</p>
  <pre><code>DJANGO
  urls.py 
  path('add-to-cart/', add_to_cart_view.as_view(), name='add-to-cart'),
  views.py 
  
  class add_to_cart_view(APIView):
      permission_classes = []
      def post(self, request, *args, **kwargs):
          id = request.data.get('id', None)
          if id is None:
              return Response({&quot;message&quot;: &quot;Invalid request&quot;}, status=HTTP_400_BAD_REQUEST)
  
          product = get_object_or_404(Product, id=id)
  
          requested_product = order_product.objects.create(
                  product=product,
                  user=request.user,
                  ordered=False
              )
  
          order_qs = Order.objects.filter(user=request.user, ordered=False)
          if order_qs.exists():
              order = order_qs[0]
              if not order.products.filter(product__id=requested_product.id).exists():
                  order.products.add(requested_product)
            
                  return Response(status=HTTP_200_OK)
  
          else:
              ordered_date = timezone.now()
              order = Order.objects.create(
              user=request.user, ordered_date=ordered_date)
              order.products.add(order_product)
              return Response(status=HTTP_200_OK)
  </code></pre>
  <pre><code>REACT
  export const AddToCartURL = &quot;http://127.0.0.1:8000&quot;;
  
  const handleAddToCartURL = async (id) =&gt; {
              try {
                  const res = await axios.post(`${AddToCartURL}/add-to-cart/`, {id});
                  setProduct(res.data);
                  console.log(res.data)
              }
              catch (err) {
  
              }
          };
  
  
      return(
          &lt;Box sx={{ flexGrow: 1 }} m={4}&gt;
              &lt;Grid container spacing={3}&gt;
                  &lt;Grid item xs={9}&gt;
                      &lt;Item&gt;
                         &lt;img src={product.image} alt=&quot;&quot; /&gt;
                         &lt;div&gt;
                             &lt;p&gt;{product.description}&lt;/p&gt;
                             &lt;p&gt;{product.label}&lt;/p&gt;
                             &lt;p&gt;{product.price}&lt;/p&gt;
  
                         &lt;/div&gt;
                         &lt;button color='primary' onClick={() =&gt; handleAddToCartURL(product.id)}&gt;
                              &lt;AddShoppingCartIcon /&gt;
                          &lt;/button&gt;  
                      &lt;/Item&gt;                  
                  &lt;/Grid&gt;
                  &lt;Grid item xs&gt;
                      &lt;Item&gt;Similar Products&lt;/Item&gt;
                  &lt;/Grid&gt;
              &lt;/Grid&gt;
      
        &lt;/Box&gt;
      )
  }
  export default ProductDetails
  </code></pre>
  <p>I really dont't understand why it is returning anonymous user from django. Please how do I fix it?</p>