improvement of a database register function Here is a function in a django rest api, that takes data in json and registers a category for a given website. I'd like to merge two try...except parts that check 'site_id' and 'category_id' but I am not sure how to do it. Also any other suggestion to make it pythonic is appreciated:

@myql_view(['POST'])                                                             
def register_category(request):                                                                    

    try:                                                                        
        site_id = request.data['site_id']                                       
    except Exception as e:                                                      
        print('Request error or missing site_id:', e)                           
        return Response(status=status.HTTP_400_BAD_REQUEST)                     

    try:                                                                        
        category_id = request.data['category_id']                               
    except Exception as e:                                                      
        print('Request error or missing category_id', e)                        
        return Response(status=status.HTTP_400_BAD_REQUEST)                     

    cursor = connection.cursor()                                                
    try:                                                                        
        cursor.execute('INSERT INTO productCategory (site_id, category_id) VALUES (%s, %s)',
                       [site_id, category_id])                                  
    except Exception as e:                                                      
        print('Could not register product', e)                                  
        return Response(status=status.HTTP_409_CONFLICT)                        
    finally:                                                                    
        cursor.close()                                                          

    return Response(status=status.HTTP_200_OK)
You can actually merge them pretty easily. They're very similar, with just a minor difference in the strings that determine what key failed. But luckily you can get the bad key by using the message attribute of the exception:]

try:                                                                        
    site_id = request.data['site_id']                                       
    category_id = request.data['category_id']                               
except Exception as e:                                                      
    print('Request error or missing {}:'.format(e.message), e)                           
    return Response(status=status.HTTP_400_BAD_REQUEST)                     


Now they both work the same except that the missing key is taken out of your exception e and formatted into the message you print for the user. 

I also think you should except KeyError, not just any old Exception. Using the latter would mask typos, while the former deals specifically with the typeof exception you expect to appear.